=================Variables ===================
1. Naming rules
It can start with a letter and underscore, and cannot be a number. The
variable name cannot have spaces. You can use "_" to replace
Python keywords and variable names. You cannot use
spaces on both sides of "=", but you cannot separate lines. Try to standardize it as much as possible!
[Short and highly readable, lowercase as much as possible, and lowercase l and o are easy to read with caution]
2. Shared References
Multiple variables refer to the same object
0-255Python automatically caches
3.is determines whether the reference object addresses are the same
{ name1=’tom’ name2=’tom’
print( name1 is name2 ) //true}
4. Detect the address value of the object
{
print( id(name1) )
}
===================Data Type====================
1. String
– Capitalize the first letter of each word (title):
{ name=”kevin mahone”
print(name.title) }
– Uniform upper/lower case (upper/lower)
– Merge (+):
{ first_name=”Kevin”
last_name=” Mahone”
full_name=first_name+last_name
print(“hello”+full_name+”!”) [splicing]}
– tabs/newlines add spaces (\t / \n) [if used in combination, if you first refer to a table and then a newline, then tab The character \t disappears! ]
– Temporarily delete whitespace (strip[before and after], lstrip[before], rstrip[after])
{
Lan=” Python ”
print(Lan.lstrip()) Delete the leading whitespace
If you want to delete it permanently, assign it to the variable
Lan=Lan. lstrip() }
[When using strings, pay attention to the pairing of " " and ' '! 】
2. Integer
【Reserve one bit for integer division】
3. Floating point [Python can be directly operated according to the order, "**" means the power such as 2**3 is: 8]
[ a. Use the str() function to avoid errors:
{ age=23
print(“Happy “+age+”rd Birthday!”) will report an error because Python doesn’t know if it is 23 or 2 and 3
must be str(age) }
b. In python2 If no decimal is specified, the decimal part is omitted
{3/2=1; 3.0/2=1.5}
]
4. Boolean: True /False
//Essence: False: 0, 0.0, None, " " (empty string),, () (empty tuple)
5. Empty object: None
6. Sequence: list (list), tuple (tuple), dictionary (dictionary), str(), range()
7.
str_int=”13”
str_float= "3.14"
num=12
String to int
str_new=int(str_int)
print(type(str_new))
String to float
print(type(float(str_float)))
int to str
print(type(str(num)))
eval() //Convert the string to the appropriate type based on the string value
print(type(eval(“11”)))
==============================================
3. Comments (#)
# The following content will be ignored by the Python interpreter, and should be developed to write clear and concise comments
4. Zen of Python
import this to view