Maison >développement back-end >Tutoriel Python >Jour #Partie || revisiter Python à partir de zéro
Day #1 - Simple Python projects
print("Hello, World!")
What is Python?
Python is a popular programming language. It was created by Guido van Rossum, and released in 1991.
It is used for:
To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):
C:\\Users\\_Your Name_\>python --version
To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:
python --version
As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:
>>> print("Hello, World!") Hello, World!
Or by creating a python file on the server, using the .py file extension, and running it in the Command Line:
C:\Users\Your Name>python myfile.py
Creating a Comment
Comments starts with a #, and Python will ignore them:
#This is a comment print("Hello, World!")
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
x = 5 y = "John" print(x) print(y)
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:
Global Variables
x = "awesome" def myfunc(): print("Python is " + x) myfunc()
x = "awesome" def myfunc(): x = "fantastic" print("Python is " + x) myfunc() print("Python is " + x)
x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) print(type(a)) print(type(b)) print(type(c))
Random Number
import random print(random.randrange(1, 10))
_An interesting example of Slicing:
_
explain b = "Hello, World!" print(b[-5:-2])
python
b = "Hello, World!"
This line assigns the string "Hello, World!" to the variable b.
python
print(b[-5:-2])
This line prints a slice of the string b. Here's how the slicing works:
Let's visualize the string with indices:
H e l l o , W o r l d !
0 1 2 3 4 5 6 7 8 9 10 11 12
-13-12-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
So, b[-5:-2] corresponds to the characters orl from the string "Hello, World!".
Therefore, the output of print(b[-5:-2]) will be:
orl
Python Strings
Get more here
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!