Bold Style
1. Variables
Naming rules for 1.1 variables
- Variables can only contain letters, numbers, and underscores, and they cannot begin with numbers, but can be underscores or letters.
- Python keywords and function names cannot be used as variable names, such as Print functions.
- The variable name should be short and descriptive, for example, student_name will be greater than s_n Better.
- All uppercase letters are consistently treated as constants and lowercase as variables.
Assignment and call of 1.2 variable
print(student_name) Traceback (most recent call last): File "D:\Python_project\Python_learning\Chapter2_test\ann_test.py", line 3, in <module> print(student_name) NameError: name 'student_name' is not defined student_name = 'tom' # Assignment of variables print(student_name) #Call student_name variable and print: tom student_name = 'jack' # Re-pair student_name assignment print(student_name) # Call student_name variable and print: jack x,y,z = 0,1,2 print(x,y,z) Output 0 1 2
From the above code, we can get the following two points:
- Variables need to be assigned before being called, otherwise an error will be reported.
- If a variable is reassigned, the original value is replaced.
- You can also assign values to multiple variables at the same time to improve the reading experience.
Two strings
Definition of 2.1 String
# Two ways to define strings: 'Hello world!' "Hello world!" # There is also flexibility in quotation marks: "I told my friend,'Python is my favorite language!'" 'The language "Python" is named after Monty Python , not the snake.' "One of Python's strengths is its diverse and supportive community." # An incorrect string representation 'One of Python's strength is its diverse and supportive community' File "D:\Python_project\Python_learning\Chapter2_test\ann_test.py", line 3 'One of Python's strength is its diverse and supportive community' ^ SyntaxError: invalid syntax
Similarly, we summarize the definition of strings:
- String definitions can be defined in two main ways, with some flexibility
- Apostrophes cannot be added to single quote strings because Python treats the contents between the first and second single quotes as strings and the rest as Python code, which can cause errors.
2.2 String Method
Noun Interpretation (Method)
It's what happens to data in Python, so there are different ways when data types are different.
2.2. 1 Modify the case of strings
Sometimes when we store English data, we need to process the user name of each user, such as capitalizing each user's user name, or lowercase or even capitalize it all. How do we achieve our demands?
At this point, the corresponding methods in the string can meet the requirements, which are as follows:
- . title(): Ability to capitalize the first letter in a string
- . upper(): Ability to capitalize all letters in a string
- . lower(): Ability to lower case all letters in a string
name = 'tom' print(name.title()) Output: Tom print(name.upper()) output: TOM print(name.lower()) Output: tom
2.2. 2 Delete blanks in strings
When a user registers for a Web site, it is often necessary for the site to check its user name to avoid duplication. At the same time, the vast majority of duplicates add several blanks to the left or right of the existing user name, while deleting extra blanks from user input data in Python is a snap in the back.
There are three main approaches, corresponding to three different situations:
- . rstrip(): Deletes the rightmost white space in the string.
- . lstrip(): Delete the leftmost white space in the string
- . strip(): Deletes the leftmost and rightmost whitespace of a string
a = ' Hello world!' print(a) Output: Hello world! print(a.lstrip()) Output: Hello world! b = 'Hello world! ' print(b) Output: Hello world! (End position) print(b.rstrip()) Output: Hello world(End position) c = ' Hello world! ' print(c) Output: Hello world! (End position) print(c.strip()) output:Hello world(End position)
2.3 String Expansion
2.3. 1 How to use variables in strings
Preface: In general, calling a defined variable in a string will only show its variable name, not the value assigned by the variable.
Therefore, if we want to call a defined variable in a string, we need to use the f string (Python 3.5 or more) and brace the variable that will be used in the string.
name = 'tom' language = 'Python' message = f "{name.title()}'s favorite language is {language.lower()!}" print(message) Output: Tom's favorite language is python!
2.3. 2 Improve your reading experience with white space characters
In Python, whitespace refers generally to non-printing fonts, such as spaces, tabs, and line breaks.
At the same time, using whitespace characters in the string can help users improve their reading experience.
First, let's introduce the types of whitespace characters.
- Blank, which everyone can understand, is just press a space.
- The line break \n, as the name implies, jumps to the next line.
- The tab \t inserts a gap between the previous field and the next field. Note: Gaps are consistent
# Unmodified display print('Languages:Python C Java script R') Output: Languages:Python C Java script R print('Languages:\n\tPython\n\tC\n\tJava script\n\tR') Output: Languages: Python C Java script R
By comparison, it is not difficult to find that proper use of whitespace characters helps us to view the data more intuitively.
Number of three
In programming, numbers are often used to record scores, represent visual data, store Web application information, and so on. Therefore, we need to understand how to use numbers in Python to perform the processing of numbers as we expect.
Number of supported 3.1 Python
- Integer: Number without decimal point
- Floating point number: number with decimal point
3.2 Python Supported Operations
There are three types of operations supported by numbers:
- Four Operations (+ - * /)
- Multiplication (**)
- Ordered operations: that is, parentheses () for the order of mathematical operations
Here, the operation can be performed not only between integers, but also between floating-point numbers and integers, but also between floating-point numbers with slightly different results.
#Operations of Integers and Integers print(3+3) Output: 6 print(3-2) Output: 1 print(3*3) Output: 9 print(6/2) Output: 3 print(3**2) output:9 print((3+2)*4) output:20 #Operation of Integers and Floating Points print(3*0.5) Output: 1.5 print(3/0.5) Output: 6 print(3+0.5) Output: 3.5 print(3-0.5) Output: 2.5 print(9**0.5) output:3.0 print(3.0 ** 2) Output: 9.0 print((3+0.5)*4) output:14.0 #Operations between floating-point numbers and floating-point numbers print(0.5+0.5) Output: 1.0 print(3*0.1) Output: 0.30000000000000004 print(0.3-0.2) Output: 0.09999999999999998 print(0.6/0.2) Output: 2.9999999999999996 print(0.25**0.5) Output: 0.5 print((0.5+0.5)*2.0) Output: 2.0
In summary, it supports three operations regardless of the number, but the results of the data are slightly different.
- When integer and integer operations, the result is an integer.
- When integers and floating-point numbers are computed, the result is a floating-point number
- When working between floating-point numbers and floating-point numbers, based on the internal computer system, the results can sometimes be strange.
Four Comments
Definition:
Explains what the code does and how it does it
Just put a pound sign in front of the text
Effect
1. Favor your own irregular review
2. Convenient cooperation with other programmers
# So we must set up the correct annotation habits