How to master Python
This article mainly introduces how to master the relevant knowledge of Python, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to master Python. Let's take a look at it.
Python Foundation 1. Variable
You can think of a variable as a word used to store values. Let's look at an example.
It is easy to define a variable in Python and assign a value to it. If you want to store the number 1 into the variable "one", let's try it:
Python
One = 1
Isn't it super easy? You just need to assign a value of 1 to the variable "one".
Python
Two = 2
Some_number = 10000
You can assign any value to any other variable as long as you want. As you can see from above, the variable "two" stores integer variable 2 and the variable "some_number" stores 10000.
In addition to integers, we can use Boolean values (True/Flase), strings, floating-point types, and other data types.
Python
# booleanstrue_boolean = Truefalse_boolean = False# stringmy_name = "Leandro Tk" # floatbook_price = 15.80
two。 Control flow: conditional statement
"If" uses an expression to determine whether a statement is True or False, and if it is True, then execute the code in if, as shown in the following example:
Python
If True:
Print ("Hello Python If") if 2 > 1:
Print ("2 is greater than 1")
2 is larger than 1, so the print code is executed.
When the expression in "if" is false, the "else" statement will be executed.
Python
If 1 > 2:
Print ("1 is greater than 2") else:
Print ("1 is not greater than 2")
1 is smaller than 2, so the code in "else" executes.
You can also use the "elif" statement:
Python
If 1 > 2:
Print ("1 is greater than 2") elif 2 > 1:
Print ("1 is not greater than 2") else:
Print ("1 is equal to 2")
3. Loop and iteration
In Python, we can iterate in different forms. I'll talk about while and for.
While loop: when the statement is True, the code block inside the while executes. So the following code will print out 1 to 10.
Python
Num = 1while num