Related links
Python You can refer to the development manual : Before coding, mumble 、Python Style guide 、Python Coding standards (Google)
print() # Print
''' print("hello", end="\t") print("world Don't wrap ") # hello world Don't wrap '''
chr(i) # Return integer i The corresponding Unicode character . Its function and ord() contrary
ord(c) # Return character c The corresponding Unicode The number . Its function and chr() contrary
id() # Return memory address
type() # Return variable type
len() # Get object length , Cannot be used for int,float,bool type
# Exception trapping
try:
print(' My name is zhang SAN ')
except Exception as e:
print(e)
finally:
pass
# Type conversion
from decimal import Decimal # It is usually placed at the beginning of the document
Decimal('1.1') # To Decimal type , It can solve the problem of floating-point calculation accuracy
str() # To string str
int() # Convert to plastic int, Parameters can only be numbers ( Numbers in string format can also )
float() # Convert to floating point float, Parameters can only be numbers ( Numbers in string format can also )
bool() # Change to Boolean bool - 0 representative False, Other numbers represent True
""" @author GroupiesM @date 2022/4/26 17:28 @introduction """
print("hello\rworld") # enter
print("hello\nworld") # Line break
print("hello\tworld") # tabs
print("hello\rworld\rpython") # retracement python (world Cover hello,python Cover world)
print("hello\bworld") # Backspace hellworld
''' print Each default line break end="\t" No line break '''
print("hello", end="\t")
print("world Don't wrap ") # hello world Don't wrap
""" @author GroupiesM @date 2022/4/27 09:41 @introduction """
print(chr(0b100111001011000)) # ride (0b For binary ) Default unicode code
print(ord(" ride ")) # 20056 ( Decimal system )-> 100111001011000(unicode code )
""" @author GroupiesM @date 2022/4/27 10:04 @introduction """
name = "groupies";
# Single-line comments
''' Multiline comment 1 '''
""" Multiline comment 2 """
""" @author GroupiesM @date 2022/4/27 09:45 @introduction Identifiers and reserved words """
import keyword
# Mode one
print(keyword.kwlist)
# Mode two
print(help("keywords"))
""" Reserved words ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield'] """
''' identifier Letter 、 Numbers 、 Underline _ Cannot start with a number It can't be reserved Case sensitive '''
""" @author GroupiesM @date 2022/4/27 09:48 @introduction id() Get memory address type() Get variable type """
name = 'groupies' # Variable assignment character string - Single quotation marks
name = "groupies" # Variable assignment character string - Double quotes
name = '''groupies''' # Variable assignment character string - Three quotes
job: str = ' Research and development ' # Variable assignment ( Specify the type )
age: int = 16; # Variable replication (int type )
print(' full name :', name, ';\t Work :', job, ';\t Age :', age, ";") # full name : groupies ; Work : Development ; Age : 16 ;
print(id(name)) # 4377113712 Memory address
print(type(name)) # <class 'str'> Variable type
''' Batch definition variables '''
name, age, money = ' Zhang San ', 18, 2000
print(name, age, money) # Zhang San 18 2000
''' Reflection gets all variables 、 expression 、 function '''
print(locals())
''' {'__name__': '__main__', '__doc__': ' @author GroupiesM @date 2022/4/27 09:48 @introduction id() Get memory address type() Get variable type ', '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x10280a2e0>, '__spec__': None, '__annotations__': {'job': <class 'str'>, 'age': <class 'int'>}, '__builtins__': <module 'builtins' (built-in)>, '__file__': '/Users/d/workspace/python/pybase/src/com/groupies/part01 Basics /05. Definition of variables .py', '__cached__': None, 'name': ' Zhang San ', 'job': ' Research and development ', 'age': 18, 'money': 2000} '''
""" @author GroupiesM @date 2022/4/27 10:06 @introduction plastic int -> 14 floating-point float -> 3.14 Boolean type bool -> true/false character string str -> ' Zhang San ' # Hexadecimal : 0x # octal : 0o # Binary system : 0b """
""" @author GroupiesM @date 2022/4/27 10:20 @introduction # Hexadecimal : 0x # octal : 0o # Binary system : 0b Basic numbers : Decimal system : \d Binary system : [12] octal : [1-7] Hexadecimal : [\dABCDEF] """
n1 = 90
n2 = 60
print(n1, type(n1)) # 90 <class 'int'>
print(n2, type(n2)) # 60 <class 'int'>
print(" Hexadecimal ", 0x1EAF) # Hexadecimal 7855
print(" Decimal system ", 118) # Decimal system 118
print(" octal ", 0o176) # octal 126
print(" Binary system ", 0b10101111) # Binary system 175
""" @author GroupiesM @date 2022/4/27 10:23 @introduction Floating point type Floating point numbers consist of integer and decimal parts Floating point number storage is not accurate : When using floating point numbers , The number of decimal places may be uncertain Solution : Import decimal modular from decimal import Decimal Decimal(1'1)+Decimal('2.1') """
from decimal import Decimal # It is usually placed at the beginning of the document
a = 3.14159
print(a, type(a)) # 3.14159 <class 'float'>
""" Floating point number storage is not accurate : When using floating point numbers , The number of decimal places may be uncertain """
print(1.1 + 2.2) # 3.3000000000000003
print(1.1 + 2.1) # 3.2
""" Solution : Import decimal """
print(Decimal('1.1') + Decimal('2.2')) # 3.3
""" @author GroupiesM @date 2022/4/27 10:31 @introduction Boolean type True representative 1 False representative 0 """
# True Non empty object 、 Or not 0 Numbers
print(True)
print(bool(1))
print(bool("abc"))
print(bool([1,2,3]))
# False An empty object 、 or 0、0.0
print(bool(0))
print(bool(0.0))
print(bool(None))
print(bool(''))
print(bool([])) # An empty list
print(bool(list())) # An empty list
print(bool(())) # An empty tuple
print(bool({
})) # An empty dictionary
print(bool(dict())) # An empty dictionary
print(bool(set())) # Empty set
print(int(True)) # 1
print(True + 1) # 2
print(False + 1) # 1
""" @author GroupiesM @date 2022/4/27 10:34 @introduction """
str1 = ' Zhang San '
str2 = " Zhang San "
str3 = ''' Zhang San '''
str4 = """ Zhang 3、 ... and """ # Three quotation marks can be used to input multiple lines of string , Other methods are not allowed
print(str1, type(str1)) # Zhang San <class 'str'>
print(str2, type(str2)) # Zhang San <class 'str'>
print(str3, type(str3)) # Zhang San <class 'str'>
print(str4, type(str4))
""" Zhang 3、 ... and <class 'str'> """
""" @author GroupiesM @date 2022/4/27 10:37 @introduction str() Convert other types to strings 1. You can also use quotation marks to convert exp: str(123) '123' int() other -> plastic 1. character string -> plastic ( The content of the string must be an integer , Otherwise, you cannot convert ) 2. floating-point -> plastic ( Rounding off ) exp: int('123') int(9.8) float() other -> floating-point 1. character string -> floating-point (x) 2. plastic -> floating-point ( At the end of .0) exp: float('9.9') float(9) bool() other -> Boolean type 1. 0 -> Boolean (False) 2. other -> Boolean (True) # Including strings , floating-point ,int All kinds of exp: bool('0') -> False bool(0) -> True """
name = ' Zhang San '
age = 20
print(type(name), type(age)) # <class 'str'> <class 'int'>
# When str The type and int Type connection , Report errors , Solution : Type conversion
try:
print(' My name is ' + name + ' This year, ' + age + ' year ') # can only concatenate str (not "int") to str
except Exception as e:
print(e)
print(' My name is ' + name + ' This year, ' + str(age) + ' year ')
print('---------str() Turn other types into str type ----')
a = 10
b = 198.8
c = False
print(type(a), type(b), type(c))
# 10 198.8 False <class 'str'> <class 'str'> <class 'str'>
print(str(a), str(b), str(c), type(str(a)), type(str(b)), type(str(c)))
print('---------int() Turn other types into int type ----')
s1 = '128'
f1 = 98.7
s2 = '76.77' # x : Decimal string cannot be converted
ff = True
s3 = 'hello' # x
print(type(s1), type(f1), type(s2), type(ff), type(s3))
print(int(s1), type(int(s1))) # 128 <class 'int'>
print(int(f1), type(int(f1))) # 98 <class 'int'>
""" invalid literal for int() with base 10: '76.77' character string ( except '\d') Type decimal cannot be converted int """
try:
print(int(s2), type(int(s2)))
except Exception as e:
print(e)
print(int(ff), type(int(ff))) # 1 <class 'int'>
""" invalid literal for int() with base 10: 'hello' character string ( except '\d') Can't turn int """
try:
print(int(s3), type(int(s3)))
except Exception as e:
print(e)
print('---------float() Turn other types into float type ----')
s1 = '128.98'
s2 = '76'
ff = True
s3 = 'hello' # x
i = 98
print(type(s1), type(s2), type(ff), type(s3), type(i))
print(float(s1), type(float(s1)))
print(float(s2), type(float(s2)))
print(float(ff), type(float(ff)))
""" could not convert string to float: 'hello' character string ( except '\d+.\d+') Type cannot be converted float """
try:
print(float(s3), type(float(s3)))
except Exception as e:
print(e)
print(float(i), type(float(i)))
""" @author GroupiesM @date 2022/4/27 12:05 @introduction input function effect : Receive input from the user return type : The type of input value is str Value storage : Use = Store the input value """
inStr = input();
print(inStr)
""" @author GroupiesM @date 2022/4/27 12:11 @introduction """
a = input(" Please enter the first addend :") # 5
b = input(" Please enter the second addend :") # 10
print(a + b) # 510, Default string concatenation
print(int(a) + int(b)) # 15
22/06/27
M