First, define variables
print (' Hello World \ n ')
Defining Variables
name= ' Tambaoliang ' #定义字符串一定加 '
age=28
quotation marks use
words= "I ' m Tambaoliang" #字符串里有 ', Outside with ""
words2= ' You ' are very handsome ' #字符串里有 ', outside with '
words3= ' i ' m shanbl,i Love "python" "#字符串既有" and " ", outside with ' or ' ""
"' Three single quotes to annotate code '
Enter and assign a value
name = input (' Please enter your name: ') #输入内容并将值赋予变量
View Variable Types
type (name)
type (age)
Second, List
Define a list
stus=[' Tambaoliang ', ' Wongduan earthquake ', ' Zhou Yifan ']#定义一个列表, subscript starting from 0
Increase
Stus.append (' Jangchul ')#append在列表末尾增加一个元素
Stus.insert (0, ' ya men ')#inset在指定为增加元素
Delete
Stus.pop (2)#删除指定位置
Stus.remove (' Jangchul ')#删除指定元素
Del Stus[1]#删除指定位置
Stus.clear ()#清空整个list
Modify
stus[1]= ' er sister '#直接通过下标定位修改
Check
Print (Stus[1])
Print (Stus[-1])#下标写-1 Take the last element
Stus.count (' two sisters ')#统计出现的次数
Stus.index (' two sisters ')#返回查询元素第一个出现的下标, if the element does not exist, it will be an error
Sort
Stus.reverse ()#反转列表
nums=[1,3,9,22,55,77,2,5,]
Nums.sort ()#升序
Nums.sort (Reverse=true)#降序
Merging
stus2=[' AA ', ' BB ']
Stus.extend (STUS2)#把stus2合并到stus
Stus3=stus+stus2#合并两个list
three-dimensional arrays
My =[
[1,2,3,4],
[' Name ', ' age ', ' sex ', ' haha ', [' A ', ' B ', ' C ', ' d '],
888,
[x]
]
My ([1],[4],[0])#取a
My.insert ([1][4][3]) #取到字母d
My[1][4].append (' e ') #定位到列表 and add e
my[1][2]= ' gender ' If Username in stus # Determines whether a variable is in the list
if username not in stus #判断某个变量是否不在列表中
Print (len (my)) #查看变量的元素个数, also the length
Iii. Conditions of Judgment
< Span style= "font-size:13px" > python conditional only if elif else
a=input (' Enter the value of a: ')
b=input (' please enter the value of B: ')
if a==b:
print (' a==b ')
elif a<b:
print (' a<b ')
#elif ... #可以有无限多个判断
else: #其余情况 without conditions
print (' A>b ' )
#if can be nested if
Four, while loop
Count=1
While count<=10: #while需要自己定义初始值, step, end value
Print (' Hello this is the first ' +count+ ')
Count=count+1
Else #正常循环完, perform else
V. FOR loop
Import Random
Num=random.randint (1,100)
For I in range (7):#range是多少, just loop a few times, variable arbitrarily, don't need to define the initial value, starting from 1
Guss = input (' Please enter a number within 100: ')
Guss = Int (Guss)
If Guss==num:
Print (' Congratulations guessed ')
Break#break立即结束循环当前跳出, just jump 1 levels.
Elif Guss>num:
Print (' Guess big ')
Continue #continue跳出本次循环, proceed to the next
else:
Print (' Guess small ')
Continue
Count+=1
else: #正常执行完, perform else
Print (' Sorry times out ')
Print (' Game over! ')
Six, string formatting
Import datetime
user= ' Tambaoliang '
today =datetime.datetime.today () #date类型
today = str (today) #转换为str
msg = ' welcome ' +user+ ' Today's date is ' +today #通过 + stitching string
Print (msg)
print (type user )
print (Today )
method Two: Pass the% pre-defined variable value
import datetime
user= ' Tambaoliang '
today = Datetime.datetime.today () #date类型
age =18
score=99.96999
msg = ' Welcome%s login, today's date is%s '% (user,today) #%s placeholder, string type, can also be substituted for other types, Magnum
MSG2 = ' your age is%d, your score is%.2f '% (age,score ) #%d integer placeholder,%f floating-point placeholder,%.2f means reserved two decimal places
print (Round (score,2)) #round保留几位小数
Vii. Other
name=input (' Please enter name: ')
age=input (' Age: ') #input输入内容默认为str
Age=int (age) #转换为int
Import Random
Num=random.randint (1,100) #使用random生成随机数
elif score>80 and score==80: #并且 connected with and
if sex== ' male ' or sex== ' female ': #或者 connected with or
#% can be used as escape character
Count+=1
Count-=1
Count*=1
Count/=1
Getting Started with Python