Detailed Explanation of Variables and Simple Number Types of python
- 2021-11-30 00:43:46
- OfStack
1. Variables
Each variable stores 1 value Variables can be modified at any time in the program, but Python will always record the latest value of variables
message = "Hello Huang ZB!"
print(message)
message = "Goodbye Huang ZB!"
print(message)
1.1 Avoid naming errors when using variable names
Check Traceback to see the error
message = "Hello Huang ZB!"
print(mesage)
2. String
Def: A string is a string of characters. Both double quotation marks and single quotation marks can represent
2.1 How to modify the case of a string
name = "huang zhibin"
print(name.title()) #title() Function: Change the first letter of each word to uppercase
Huang Zhibin
Other methods:
name = "huang zhibin"
print(name.title()) #title() Function: Change the first letter of each word to uppercase
print(name.upper()) #upper() The string () function converts all string contents to uppercase
print(name.lower()) #lower() The string () function converts all string contents to lowercase
Huang Zhibin
HUANG ZHIBIN
huang zhibin
2.2 Merge Strings
Methods: Splicing
first_name = 'huang'
last_name = 'zhibin'
full_name = first_name + ' ' + last_name
print('Hello, ' + full_name.title() + '!') # This + Indispensable
Hello, Huang Zhibin!
2.3 Use tabs or line breaks to add white space
Add tabs to the string, using\ t (also understood as carry)
print("python")
print("\tpython") # \t Representing tabs
python
python
Add a newline character to the string, using\ n
print("Languages:\nPython\nC\nJavaScript") # \n Represents a newline character
Languages:
Python
C
JavaScript
The same 1 string can contain both tab and newline string "\ n\ t": Wrap python to the next 1 line
print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
2.4 Remove Blanks
python can find excess white space at the beginning and end of a string. To ensure that there is no white space at the beginning and end, use rstrip () To ensure that there is no white space at the beginning, use the method lstrip () At the same time, eliminate the white space at both ends of the string, using the method strip ()
information = ' Life is too short, I learn python '
print(information.rstrip())
print(information.lstrip())
print(information.strip())
Life is too short, I learn python
Life is too short, I learn python # The right space still exists!
Life is too short, I learn python
2.5 Avoid syntax errors when using strings
Syntax error is also an important check index when modifying the program again
3. Number type
3.1 Integer
>>> 2+3
5
>>> 5-6
-1
>>> 4*5
20
>>> 36/6
6.0
>>> 3**2
9
>>> 2+2**2
6
>>> (2+2)*2
8
3.2 Floating point numbers
message = "Hello Huang ZB!"
print(mesage)
0
Keep two decimal places
message = "Hello Huang ZB!"
print(mesage)
1
3.3 Complex
message = "Hello Huang ZB!"
print(mesage)
2
3.4 Use the function str () to avoid type errors
age = 21
message = "Happy " + str(age) + "rd Birthday!" # Convert non-string values to strings
print(message)
Happy 21rd Birthday!
4. Notes
Single-line comment
#
Multi-line comment
'''
Comments cannot be nested! ! ! ! ! In fact, in fact, the
5. Zen of python
message = "Hello Huang ZB!"
print(mesage)
4
Summarize
This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!