This article mainly shares with you a detailed explanation of the basics of Python syntax, hoping to help you.
Python comments
'''多行注释''' 井号单行注释
Add to the file header #coding=utf-8
Add #-*- coding:utf-8 -*-
to the file header (python recommended )
python3 input: The value entered in input is the value of the variable on the left
heigh=input("请输入值:") print("heigh:%d"%heigh)
Result It’s
#heigh:3
Python2 input: The value entered in input is executed as a string of codes, so raw_input should be used. Input
#coding=utf-8 a=raw_input("输入值:") print a b=input("请输入:") print
and the result is
输入值:12 12 请输入:1+5 6
3. Python outputs the values of multiple variables at the same time
Name=”shushu”
Age=20
addr=”Dalian, Sichuan”
Print("Name: %s, Age: %d, Address: %s" %(name,age,addr))
The result is:
Name: shushu, age: 20, address: Dalian, Sichuan
and and
or or
nonnot
if age>18: print("成年了") else: print("未成年")
if age>18: print("成年了") elif age>10: print("少年") else: print("儿童")
i=1; while i<10: print(i) i+=1
In Python, the for loop can variable any sequence of items, such as a List or a string, etc.
for 临时变量 in 列表或者字符串等: 循环满足条件时执行的代码 else: 循环不满足条件时执行的代码
For example:
name = 'dongGe' for x in name: print(x)
will output
d o n g G e
Related recommendations:
python syntax marshmallow Description
Python syntax exercise--while loop
Python syntax quick start guide
The above is the detailed content of Detailed explanation of Python syntax basics. For more information, please follow other related articles on the PHP Chinese website!