一、What are Data Types?¶
In Python, data types are like labels for different “boxes” that tell Python what’s inside the box and what operations can be performed on it. For example, mathematical numbers, text, and “yes/no” judgments all need different “boxes” to store them. Understanding data types is fundamental to programming because different types of data support completely different operations.
二、Integers (int): The Home for Numbers¶
Integers are positive, negative integers, and zero from mathematics, such as 0, 5, -3, 1000, etc. In Python, integers have no size limit (for example, you can write a very large number like 1000000000000, and Python can handle it).
1. Creating Integer Variables¶
Assign integers to variables using the assignment operator =, for example:
age = 18 # Age is an integer
temperature = -5 # Temperature can be negative
count = 0 # Initial count
2. Basic Operations on Integers¶
Python supports common mathematical operations using + (addition), - (subtraction), * (multiplication), and / (division):
a = 10
b = 5
print(a + b) # 15 (addition)
print(a - b) # 5 (subtraction)
print(a * b) # 50 (multiplication)
print(a / b) # 2.0 (division, note the result is a float; division in Python 3 returns a decimal by default)
- Modulo operation (
%): Find the remainder of division, e.g.,7 % 3results in1(7 divided by 3 gives a quotient of 2 and remainder 1). - Floor division (
//): Keep only the integer part of the quotient, e.g.,7 // 3results in2(7 divided by 3 is 2.333, so only 2 is kept).
3. Converting to Integers¶
To convert data from other types to integers (e.g., converting the string "123" to an integer), use the int() function:
num_str = "123"
num_int = int(num_str) # Convert the string "123" to the integer 123
print(num_int + 1) # Output: 124
三、Strings (str): The Container for Text¶
Strings are text enclosed in quotes (single quotes ' or double quotes "), such as "Hello", 'Python', "I love coding". They are used to store text information like names, sentences, URLs, etc.
1. Creating String Variables¶
Wrap text in single or double quotes, for example:
name = "Alice" # Double quotes
message = 'Python is fun!' # Single quotes
empty_str = "" # Empty string
Note: Single and double quotes can be mixed, but quotes cannot be mixed within the same string (e.g.,
"He said 'Hi'"is valid because the outer quotes are double and the inner are single;'He said "Hi"'is also valid, as long as quotes are paired).
2. String Concatenation¶
Use the + operator to concatenate multiple strings, e.g.:
first_name = "Zhang"
last_name = "San"
full_name = first_name + " " + last_name # Add a space in between
print(full_name) # Output: "Zhang San"
3. String Length¶
Use the len() function to get the number of characters in a string (spaces count as one character):
text = "Hello"
print(len(text)) # Output: 5 (H, e, l, l, o: 5 characters total)
4. String Indexing¶
Each character in a string has a “position number” called an index, starting from 0. For example, in "Python":
- The 0th character is 'P'
- The 1st character is 'y'
- The 2nd character is 't'
Extract individual characters using indexing:
s = "Python"
print(s[0]) # Output: 'P'
print(s[2]) # Output: 't'
5. Converting to Strings¶
To convert integers or other types to strings, use the str() function:
age = 18
age_str = str(age) # Convert integer 18 to string "18"
print("My age is " + age_str) # Output: "My age is 18"
四、Booleans (bool): The Switch for True/False¶
Booleans have only two values: True (True) and False (False), used to represent “yes/no” or “true/false” logical judgments. Note: The first letters of True and False must be capitalized; otherwise, an error will occur (e.g., true or false is incorrect).
1. Creating Boolean Variables¶
Assign True or False directly, for example:
is_student = True # Is a student
is_verified = False # Not verified
2. Logical Judgments¶
The most common use of booleans is conditional judgment, such as checking if a number is greater than a certain value:
score = 85
is_pass = score > 60 # 85 > 60 results in True
print(is_pass) # Output: True
You can also negate a boolean with not:
is_active = True
print(not is_active) # Output: False
五、Practice Exercises: Let’s Try It!¶
- Create an integer variable
yearwith the value 2024, and print the result ofyear + 1. - Create a string variable
languagewith the value"Python", and print its length usinglen(). - Create a boolean variable
is_rainingwith the valueFalse, and print the result ofnot is_raining. - Try concatenating the strings
"Hello, " + "World!"and print the result.
六、Summary¶
These are the three most basic data types in Python:
- Integers (int): Handle numerical calculations (addition, subtraction, multiplication, division, modulo, etc.).
- Strings (str): Handle text information (concatenation, length, indexing, etc.).
- Booleans (bool): Handle logical judgments (yes/no, true/false).
Mastering these is the first step in learning Python. We will later explore more complex data types (e.g., lists, dictionaries), but basic data types are the “foundation” for all operations!