variable
- Variable names are like the names of our real society. When a value is assigned to a name, Ta will be stored in the memory, which is called a variable . In most languages, this behavior is called " assigning a value to a variable " or " storing a value in a variable "
- Python is slightly different from most other computer languages. Ta does not store the value in a variable, but rather pastes the name on it.
- So some Python programmers will say that Python has no variables but names
Attention should be paid when using variables
1. Before using variables, you must first assign values
2. Variable names can include letters, numbers, and underscores, but variable names cannot start with numbers
3. Letters can be uppercase or lowercase, but the case is different, for example, Fish is different from fish
4. The equal sign (=) means assignment. The left side is the name, and the right side is the value. You cannot write the reverse
5. The variable naming theory can take any legal name, but as an excellent programmer, try to give a variable one Professional name
String
- So far, the string we know is everything inside the quotation marks. We also call the string text. Text and numbers are completely different.
- To tell Python that you are creating a string, you need to add quotation marks around the string. It can be single or double quotation marks, and they must be paired. You cannot use single quotation marks and double quotation marks on the other.
- What if single or double quotation marks need to appear in the string?
Method 1 : Use the transfer symbol (\) to escape the quotation marks in the string
Method 2 : Use different quotation marks to represent the string
Raw string
In the following code **\n represents the carriage return character**, which does not meet our expectations.
Solution
(1) Use a backslash to escape itself
(2) Use the original string and add it before the string r
Note : You cannot add a backslash (\)
after the original string. If you have to add a backslash (\) at the end of the original string, what method is there?
Long string
- If you want a string that spans multiple lines, such as:
I think
love is a matter between two people, what do
you think? - At this time, we need to use triple quoted strings.
Note : speak in English, everything!
Task
- Remember our first hands-on topic? This time it is required to use variables to calculate how many seconds are in a year?
Tip: You can use DaysPerYear (days per year), HoursPerDay (hours per day), MinutesPerHour (minutes per hour), SecondsPerMinute (seconds per minute) as variable names.