Python学习之路 Week one

一 打印

print("xxx")

二 变量/字符串

(一)变量作用

      用于存储东西以便之后调用

(二)变量定义

1 变量的操作

 name ="Tortoise man"

 print("My name is",name)

 name2 =name

 print("My name is",name,name2)

name ="X"

print(name,name2)

此时的name != name2

2 定义的规则

  •  变量名只能是字母、数字或下划线的任意组合
  • 变量名的首字母不能是数字
  • python关键词不能为变量(and,while,print et al.)

三 字符编码

ASCII 255=2^8-1  1byte

GB2312  7XXX

GBK1.0  2W+

GB18030 27XXX

Unicode 2bytes

utf-8  English: 1byte, Chinese:3bytes

四 注释

(一) 单行注释

 #XXX

(二) 多行注释

'XXX' or "XXX"

五 用户输入

(一)简单输入

username=input("username:")

password=input("password:")

(二)格式化输出

1 info

name =input("name:")

age =input("age:")

job =input("job")

salary =input("salary")

info='''

-------info of %s----

name =%s

age =%d

job =%s

salary =%s

'''%(name,age,job,salary)

print(info)

2 info2

name =input("name:")

age =input("age:")

job =input("job")

salary =input("salary")

info2 ='''

------info of {_name}-----

name ={_name}

age ={_age}

job ={_job}

salary ={_salary}

'''.format (_name =name,

               _age=age,

               _job=job,

               _salary=salary)

print(info2)

3 info3

name =input("name:")

age =input("age:")

job =input("job")

salary =input("salary")

info3='''

-----info of {0}-----

name={0}

age={1}

job={2}

salary={3}

'''.format(name,age,job,salary)

print(info3)

note A:打印输入类型

print(type("X"))

note B:字符变数字

int(print("X"))

六 密码明文变密文

import getpass

username = import(“username:”)

password =getpass.getpass (“password:”)

七 认证用户和密码

_username = input(“username:”)

_password =input (“password:”)

If _username == username and  _password == password:

   Print(“welcome user (name) login….”)

Else:

   Print(“invalid username or password!”)

八 猜数字

age_of _oldboy = 56

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

else:

print(“think bigger!”)

九 while循环

(一)循环到1000

count = 0

while True:

   print(“count:”,count)

   count = count +1

   If count == 1000:

     Break

(二)正确后退出

age_of _oldboy = 56

while true:

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

  break

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

else:

print(“think bigger!”)

(三)循环恰好三次退出

age_of _oldboy = 56

count = 0

while true:

if count == 3:

break

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

  break

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

else:

print(“think bigger!”)

count =count+1

age_of _oldboy = 56

count = 0

while count<3:

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

  break

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

else:

print(“think bigger!”)

count =count+ 1

(四)尝试多次之后提醒

age_of _oldboy = 56

count = 0

while true:

if count<3:

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

  break

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

count=count+ 1

else:

  print(“you have tried too many times….”)

(五)3次之后提醒是否继续

age_of_oldboy = 56
count = 0
while count <3:
    guess_age = int(input("guess age:") )
    if guess_age == age_of_oldboy :
        print("yes, you got it. ")
        break
    elif guess_age > age_of_oldboy:
        print("think smaller...")
    else:
        print("think bigger!")
    count +=1
    if count == 3:
        countine_confirm = input("do you want to keep guessing..?")
        if countine_confirm != 'n':
            count =0

十 for循环

(一)连续打印

for i in range(10):

print(“loop”,i)

(二)跳行打印

for i in range(0,10,2):

print(“loop”,i)

十一  while循环变for循环

age_of _oldboy = 56

for i in range(3):

guess_age = int(input(“guess age:”))

if guess_age ==age_of_oldboy:

  print(“yes,you got it”)

  break

elif guess_age>age_of_oldboy:

  print(“think smaller…”)

else:

  print(“you have tried too many times….”)

十二 contine(跳出本循环,跳到下一个循环)

for I in range(0,10):

If i<3:

print(“loop”,i)

 else:

  continue

print(“hehe…”)

十三  循环嵌套

for i in range(10):

   print(“loop”,i)

  for j in range(10):

   print(j)

   if j>5

   break

猜你喜欢

转载自www.cnblogs.com/tortoise-man/p/9351691.html