Docsity

Introduction to Python 1, Summaries of Computer Science

Toggle sidebar
bg1
Introduction to Python:
Python is a high-level, interpreted programming language that emphasizes
code readability and simplicity. It was created by Guido van Rossum in 1991
and is now one of the most widely used programming languages in the world.
Python is a versatile language that can be used for a wide range of
applications, including web development, scientific computing, data analysis,
artificial intelligence, and more. It has a large standard library that provides a
variety of modules and functions for common programming tasks.
Variables and Data Types in Python:
Variables are used to store data in a program. In Python, a variable is created
when a value is assigned to it. Variable names can contain letters, numbers,
and underscores, but cannot start with a number.
Python has several built-in data types, including:
1. Numbers: Numbers in Python can be integers (whole numbers), floating-point
numbers (decimal numbers), or complex numbers (numbers with both real
and imaginary parts). For example:
makefileCopy code
x = 3 y = 3.14 z = 3 + 4j
2. Strings: Strings in Python are sequences of characters, enclosed in single or
double quotes. For example:
makefileCopy code
my_string = "Hello, world!"
3. Booleans: Booleans in Python represent true or false values. The two possible
values are True and False. For example:
pythonCopy code
my_bool = True
4. Lists: Lists in Python are ordered sequences of elements, enclosed in square
brackets. Lists can contain elements of different data types. For example:
pythonCopy code
my_list = [ 1, 2, "three", True ]
5. Tuples: Tuples in Python are similar to lists, but are immutable (cannot be
changed). Tuples are enclosed in parentheses. For example:
pf2

Partial preview of the text

Download Introduction to Python 1 and more Summaries Computer Science in PDF only on Docsity!

Introduction to Python: Python is a high-level, interpreted programming language that emphasizes code readability and simplicity. It was created by Guido van Rossum in 1991 and is now one of the most widely used programming languages in the world. Python is a versatile language that can be used for a wide range of applications, including web development, scientific computing, data analysis, artificial intelligence, and more. It has a large standard library that provides a variety of modules and functions for common programming tasks. Variables and Data Types in Python: Variables are used to store data in a program. In Python, a variable is created when a value is assigned to it. Variable names can contain letters, numbers, and underscores, but cannot start with a number. Python has several built-in data types, including:

  1. Numbers: Numbers in Python can be integers (whole numbers), floating-point numbers (decimal numbers), or complex numbers (numbers with both real and imaginary parts). For example: makefileCopy code x = 3 y = 3.14 z = 3 + 4j
  2. Strings: Strings in Python are sequences of characters, enclosed in single or double quotes. For example: makefileCopy code my_string = "Hello, world!"
  3. Booleans: Booleans in Python represent true or false values. The two possible values are True and False. For example: pythonCopy code my_bool = True
  4. Lists: Lists in Python are ordered sequences of elements, enclosed in square brackets. Lists can contain elements of different data types. For example: pythonCopy code my_list = [ 1 , 2 , "three" , True ]
  5. Tuples: Tuples in Python are similar to lists, but are immutable (cannot be changed). Tuples are enclosed in parentheses. For example:

pythonCopy code my_tuple = ( 1 , 2 , "three" , True )

  1. Sets: Sets in Python are unordered collections of unique elements, enclosed in curly braces. For example: makefileCopy code my_set = {1, 2, 3, 4}
  2. Dictionaries: Dictionaries in Python are collections of key-value pairs, enclosed in curly braces. For example: makefile my_dict = { "name" : "John" , "age" : 30, "city" : "New York" } Exercise:
  3. Create a variable called my_num and assign it a value of 5. Print the value of my_num.
  4. Create a variable called my_string and assign it a value of "Hello, world!". Print the value of my_string.
  5. Create a variable called my_bool and assign it a value of True. Print the value of my_bool.
  6. Create a list called my_list with the following elements: 1, 2, "three", True. Print the value of my_list.
  7. Create a tuple called my_tuple with the following elements: 1, 2, "three", True. Print the value of my_tuple.
  8. Create a set called my_set with the following elements: 1, 2, 3, 4. Print the value of my_set.
  9. Create a dictionary called my_dict with the following key-value pairs: "name": "John", "age": 30, "city": "New York". Print the value of my_dict.