首页 > 代码库 > python 学习分享-基础篇

python 学习分享-基础篇

1、python起手式

写下第一个代码,打印‘hello world’

print(hello world)

2、变量

变量是为了存储信息,在程序中被调用,标识数据名称或类型。

变量定义的规则:

变量名只能是字母,数字或下划线的任意组合

变量名的第一个字符不能是数字

关键字不能生命为变量

为了方便读取等,变量命名一般都遵循变量本身的意义,如名字命名为name,年龄为age等等。

name = "Laay"  #字符串类型string

age = 21  #int类型

province = "shandong"

NameOfTwinsGf = "hanmeimei"

name_of_twins_gf = "hanmeimei"

address = “shanghai”

address1 = address

address = “beijing”   #address1 赋的是值,而不是变量,所以不变

3 注释

#       单行注释,最多不超过80个字符

‘‘‘ ‘‘‘   多行注释

4 格式化字符串

name = input("input your name:")
age =int( input("input your age:"))
job = input("input your job:")

print("name is :",name)
print("age is :",age)
print("job is :",job)

msg = ‘‘‘ 
infomation of user %s:
-----------------------------
name:  %s
age :  %d 
job :  %s 
-----------------------------
‘‘‘ % (name,name,age,job)
print(msg)

5 if else条件

_username = ‘Laay’
_password = ‘Laay’
username = input(input your name :)
password = input(input your password:)
if _username == username and _password == password:
    print(login..)
else:
    print(username or password is correct...)
age = 22
guess_num = int(input(input your num:))
if guess_num == age :
    print(congratulations! you got it.)
elif guess_num >age:
    print(think smaller!)
else:
    print(think bigger!)

 

6 while 循环

age = 22
counter = 0 
for i in range(10)
    if counter<3:
        guess_num = int(input(input your num:))
        if guess_num == age :
            print(congratulations! you got it.)
            break
        elif guess_num >age:        
            print(think smaller!)
        else:
           print(think bigger!)
    else:
        continue_confirm = input(Do you want to contnue because you are stupid:)
        if countinue_confirm == y
            counter   =  0
            ciontinue
        else:
            print(bye)
            break
    count + = 1

 

python 学习分享-基础篇