python-python基础1(变量、判断、循环、模块、数据运算)

一、变量

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

info='''
--- information of %s ---
name:%s
age:%s
job:%s
''' % (name,name,age,job)

info2='''
--- information2 of {name} ---
name:{name}
age:{age}
job:{job}
'''.format(name=name,age=age,job=job)

print(info2)

 

二、if...else表达式  和 while循环

一直循环:while True

例1:

age_of_jehu=25

count=1
while count<=3:
    age=int(input("guess_age:"))
    if age==age_of_jehu:
        print("Yes,you got it.")
        break
    elif age < age_of_jehu:
        print("The number you input is smaller.")
    else:
        print("The number you input is bigger.")
    count+=1
else:
    print("You have tired too much times.")

例2:

age_of_jehu=25

count=0
while count<3:
    age=int(input("guess_age:"))
    if age==age_of_jehu:
        print("Yes,you got it.")
        break
    elif age < age_of_jehu:
        print("The number you input is smaller.")
    else:
        print("The number you input is bigger.")
    count+=1
    if count == 3:
        continue_confirm=input("Do you want to continue this game? y or n\n")
        if continue_confirm=="y":
            count=0
        else:
            print("The game is over!")

 

三、for循环

for i in range(10):
    if i<5:
        continue #不往下走了,直接进入下一次loop
    print("loop:", i )

 

编写登陆接口

  • 输入用户名密码
  • 认证成功后显示欢迎信息
  • 输错三次后锁定
username="jehu"
password="123456"

count=0
while count<5:
    username1 = input("username:")
    password1 = input("password:")
    if username1==username and password1==password:
        print("Congratulations, login successfully.")
        break
    else:
        print("wrong username or password.")
        continue1=input("Do you want to continue typing? y or n\n")
        if continue1=="n":
            break
    count+=1
else:
    print("You have made more than 5 mistakes.")

 

四、模块

Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库。

先简单认识sys和os这两个模块

import sys
import os

#打印当前文件路径
print(sys.argv)
#调用系统命令
os.system("dir")  #执行命令,不保存结果

os_read=os.popen("dir").read()
print(os_read)

 

五、数据运算

算数运算:

比较运算:

赋值运算:

逻辑运算:

成员运算:

身份运算:

位运算:

#!/usr/bin/python
  
a = 60            # 60 = 0011 1100
b = 13            # 13 = 0000 1101
c = 0
  
c = a & b;        # 12 = 0000 1100
print "Line 1 - Value of c is ", c
  
c = a | b;        # 61 = 0011 1101
print "Line 2 - Value of c is ", c
  
c = a ^ b;        # 49 = 0011 0001 #相同为0,不同为1
print "Line 3 - Value of c is ", c
  
c = ~a;           # -61 = 1100 0011
print "Line 4 - Value of c is ", c
  
c = a << 2;       # 240 = 1111 0000
print "Line 5 - Value of c is ", c
  
c = a >> 2;       # 15 = 0000 1111
print "Line 6 - Value of c is ", c

*按位取反运算规则(按位取反再加1)   详解http://blog.csdn.net/wenxinwukui234/article/details/42119265

 

运算符优先级:

 

 

 

posted @ 2020-02-13 21:25  jehuzzh  阅读(138)  评论(0)    收藏  举报