Hello everyone , I'm cabbage . Because of the needs of the project , Studying recently Python The reptiles of . This one is about Python The basic knowledge of , It is also an introductory knowledge point for learning reptiles ! If you get something after reading the article , You can support bloggers three times in a row ~, Hee hee .
int()
# Convert to an integer print(int("123")) # 123print(int(123.78)) # 123print(int(False)) # 0print(int(True)) # 1
It should be noted that :
The conversion will fail in the following two cases
float()
print(float("123.78")) # 123.78print(float(123)) # 123.0print(float(True)) # 1.0print(float(False)) # 0.0
str()
print(str(1))print(type(str(1)))print(str(1.0))print(type(str(1.0)))print(str(False))print(type(str(False)))print(str(True))print(type(str(True)))
Running results :
bool()
# The following operation results are all Falseprint(bool(0.0)) # Floating point value is 0print(bool(''))print(bool("")) # An empty string print(bool(0)) # Integer value is 0print(bool({})) # The dictionary is empty print(bool([])) # The list is empty. print(bool(())) # Tuple is empty
Summary :
Dictionary type 、 A tuple type 、 When the list type and string are empty ( That is, there is no data ), Convert to boolean type is False; Integer and floating point numbers Values for 0
when , Converting to boolean type is also False; Everything else is True
Let's first look at the operators we need to learn :
Look at their functions through the code :
a = 10b = 3print(a + b) # 13print(a - b) # 7print(a * b) # 30print(a / b) # 3.3333333333333335print(a // b) # 3print(a % b) # 1print(a ** b) # 1000print((a + b) * 5) # 65print(10 + 1.1) # 11.1print((5 + 1.5) * 2) # 13.0# Add strings message1 = "hello,"message2 = "world"print(message1 + message2)# Add numbers and strings , Direct error :TypeError: can only concatenate str (not "int") to str# print(message1 + a)# String multiplied by number , How many times will this string be repeated print(message1 * b)
It should be noted that :
/
differ Java、C Programming language , Its operation is a division operation rather than an integral division operation !Numbers and strings cannot be added
String multiplied by number , The string will be repeated
There are two main methods of assignment :
# Single assignment a = 10print(a) # 10# Multiple assignments b = c = 5print(b) # 5print(c) # 5d, e, f = 1, 2, 3print(d) # 1print(e) # 2print(f) # 3
The use of compound assignment operators is also very simple , Similar to arithmetic operators :
a = 5a += 1print(a) # 6 Equivalent to a = a + 1b = 5b -= 1print(b) # 4c = 5c *= 2print(c) # 10d = 5d /= 2print(d) # 2.5e = 5e //= 2print(e) # 2f = 5f %= 2print(f) # 1g = 5g **= 2print(g) # 25
Python The use of comparison operators in is the same as in other programming languages :
print(2 == 3) # Falseprint(2 != 3) # Trueprint(2 > 3) # Falseprint(2 >= 3) # Falseprint(2 < 3) # Trueprint(2 <= 3) # True
Before using logical operators , We need to understand their function :
The code for :
print(True and False)print(False or True)print(not False)a = 34a > 10 and print('hello world')a < 10 and print('hello world')a > 10 or print(' Hello world ')a < 10 or print(' Hello world ')
Are you right about the running results ?
Thank you for reading , Progress together , Hee hee ~
author : I'm a cabbage
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .