Chapter 1: Variables, Data Types, and Operators
Understanding the Concept of Variables and Data Types
When we write programs, we need to work with data that is stored in the computer's memory. Variables are used to represent these data and can be accessed and manipulated using their names. Python is a dynamically typed language, which means we can directly assign values to variables without declaring their types.
For example, the following code defines a variable a
and assigns it the value of an integer, 10:
a = 10
a = 10
In this example, we create a variable called a
and assign the integer value 10 to it. The type of this variable is int, representing an integer.
Common data types in Python include:
- Numbers: integers (int), floating-point numbers (float), complex numbers (complex)
- Strings (str)
- Lists (list)
- Tuples (tuple)
- Dictionaries (dict)
- Sets (set)
For example, the following code creates a list and a string variable:
my_list = [1, 2, 3, 4, 5] # list
my_string = "Hello, World!" # string
my_list = [1, 2, 3, 4, 5] # list
my_string = "Hello, World!" # string
The variable my_list
represents a list containing integers from 1 to 5. The variable my_string
represents a string containing the text "Hello, World!".
It is important to note that different types of data cannot be mixed in operations. For example, integers and strings cannot be directly added. The following code would raise a type error:
a = 10
b = "Hello"
c = a + b # Raises an error
a = 10
b = "Hello"
c = a + b # Raises an error
Therefore, when writing programs, it is necessary to choose appropriate data types based on the requirements and ensure that the data types involved in operations are consistent.
Learning Common Data Types: Numbers, Strings, Lists, Tuples, Dictionaries
Python has common data types such as numbers, strings, lists, tuples, dictionaries, and sets. Here is a brief introduction to these data types and their basic usage:
- Numbers: Numeric types such as integers, floating-point numbers, and complex numbers. They can be used for mathematical operations like addition, subtraction, multiplication, division, modulus, and exponentiation. For example:
a = 10 # integer
b = 3.14 # float
c = 2 + 3j # complex number
print(a + b) # addition
print(a % b) # modulus
print(c ** 2) # exponentiation
a = 10 # integer
b = 3.14 # float
c = 2 + 3j # complex number
print(a + b) # addition
print(a % b) # modulus
print(c ** 2) # exponentiation
- Strings: Used to represent sequences of text. String operations include concatenation, slicing, and formatting. For example:
a = "Hello"
b = "World"
print(a + " " + b) # string concatenation
print(a[1:3]) # slicing
print("My name is %s, I'm %d years old." % ("Tom", 18)) # string formatting
a = "Hello"
b = "World"
print(a + " " + b) # string concatenation
print(a[1:3]) # slicing
print("My name is %s, I'm %d years old." % ("Tom", 18)) # string formatting
- Lists: Used to store an ordered collection of items that can be of different types. List operations include adding, removing, inserting, and sorting. For example:
my_list = [1, "Hello", 3.14, True] # list
my_list.append("World") # adding data
my_list.remove(3.14) # removing data
my_list.insert(1, "Python") # inserting data
my_list.sort() # sorting
print(my_list)
my_list = [1, "Hello", 3.14, True] # list
my_list.append("World") # adding data
my_list.remove(3.14) # removing data
my_list.insert(1, "Python") # inserting data
my_list.sort() # sorting
print(my_list)
- Tuples: Similar to lists, but tuples are immutable once created. They can be seen as read-only lists. For example:
my_tuple = (1, "Hello", 3.14, True) # tuple
print(my_tuple[1]) # accessing elements
len(my_tuple) # number of elements
my_tuple = (1, "Hello", 3.14, True) # tuple
print(my_tuple[1]) # accessing elements
len(my_tuple) # number of elements
- Dictionaries: Used to store key-value pairs. Values can be accessed using their keys, and operations like adding, updating, and deleting key-value pairs can be performed. For example:
my_dict = {"name": "Tom", "age": 18, "gender": "male"} # dictionary
print(my_dict["name"]) # accessing values
my_dict["age"] = 19 # updating value
my_dict["city"] = "Beijing" # adding key-value pair
del my_dict["gender"] # deleting key-value pair
print(my_dict)
my_dict = {"name": "Tom", "age": 18, "gender": "male"} # dictionary
print(my_dict["name"]) # accessing values
my_dict["age"] = 19 # updating value
my_dict["city"] = "Beijing" # adding key-value pair
del my_dict["gender"] # deleting key-value pair
print(my_dict)
- Sets: Used to store a collection of unique elements. Set operations include intersection, union, and difference. For example:
my_set1 = set([1, 2, 3, 4, 5]) # set
my_set2 = set([4, 5, 6, 7, 8])
print(my_set1 & myset2) # intersection
print(my_set1 | my_set2) # union
print(my_set1 - my_set2) # difference
my_set1 = set([1, 2, 3, 4, 5]) # set
my_set2 = set([4, 5, 6, 7, 8])
print(my_set1 & myset2) # intersection
print(my_set1 | my_set2) # union
print(my_set1 - my_set2) # difference
These are just a few examples of the common data types in Python. Each data type has its own set of operations and methods. By understanding these data types and their usage, you can effectively work with different types of data in your Python programs.