This article source: Open source China, translator rever4433, Tocy, Tony, Nangong Ice Yu
This article link: Https://www.oschina.net/translate/learning-python-from-zero-to-hero
Original English: https://medium.freecodecamp.org/learning-python-from-zero-to-hero-120ea540b567
First question, what is Python. According to the father of Python Guido van Rossum, Python is:
a high-level programming language whose core design philosophy is code readability and syntax, allowing programmers to express their ideas with little code.
For me, the first reason to learn Python is that Python is a language that can be elegantly programmed. It can simply and naturally write code and implement my ideas.
Another reason is that we can use Python in many places: Data science, WEB development, and machine learning can all be developed using Python. Quora, Pinterest and Spotify use Python for their back-end Web development. So let's learn about Python.
Python Basics
1. Variable
You can think of variables as a word to store values. Let's look at an example.
It's easy to define a variable in Python and assign it a value. If you want to store the number 1 to the variable "one", let's try:
one = 1
It's super simple. You just need to assign the value 1 to the variable "one".
two = 2
Some_number = 10000
As long as you want, you can assign any value to any other variable . As you can see from the above, the variable "two" stores an integer variable of 2 , and the variable "some_number" stores 10000.
In addition to integers, we can also use Boolean (True/flase), string, floating-point, and other data types.
# Booleanstrue_boolean = Truefalse_boolean = false# stringmy_name = "Leandro Tk" # floatbook_price = 15.80
2. Control process: Conditional statement
"If" uses an expression to determine whether a statement is true or False, and if true, executes the code in if, as follows:
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 is executed.
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 inside "else" is executed.
You can also use the "elif" statement:
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. Loops and Iterations
In Python, we can iterate in different forms. I'll say while and for.
While loop: When the statement is True, the code block inside the while is executed. So the following code will print out 1 to 10.
num = 1while num <= 10:
Print (num)
num + + 1
While loop