首页 > 编程语言 > 详细

python学习(day1)

时间:2017-02-28 14:18:01      阅读:143      评论:0      收藏:0      [点我收藏+]

开发工具:PyCharm

1、第一个python程序:

print("Hello World!")

2、变量:只能是 数字,字母,下划线的任意组合

定义变量:

name = "deyi liu"#name:变量名,deyi liu:变量值
print("My name is",name)#调用变量

3、打印多行:

msg = ‘‘‘
 name = "deyi liu"
 age = "24"
 PIE = "常量"
‘‘‘ 
print(msg)

4、用户输入:

name = input("name:")

wu加密(getpass)

import getpass #导入getpass模块,密码加密
passworld = getpass.getpass("passworld:")

5、字符串拼接:

‘‘‘
#字符串拼接:(%s:占位符,s代表string
             %d:占位符,d代表date
             %f:占位符,f代表float)
‘‘‘
name = input("name:")#raw_input:python2.x这样写,效果等同于Python3.x的input
age = int(input("age:"))#int:integer 整型
print(type(age) , type(str(age)))#打印一个变量的数据类型
job = input("job:")
salary = input("salary:")
info = ‘‘‘
---------info of %s-----------
name = %s
age = %d
job = %s
salary =%s
‘‘‘ % (name,name,age,job,salary)
info2 = ‘‘‘
---------info of {_name}-----------
name = {_name}
age = {_age}
job = {_job}
salary = {_salary}
‘‘‘ .format(_name=name,
            _age=age,
            _job=job,
            _salary=salary)
info3 = ‘‘‘
---------info of {0}-----------
name : {0}
age : {1}
job : {2}
salary : {3}
‘‘‘ .format(name,age,job,salary)
print(info)
print(info2)
print(info3)

6、if else 判断:

import getpass
real_username = "liudeyi"
real_passworld = "Aa111111"
username = input("username:")
passworld = getpass.getpass("passworld:")#在pycharm不能执行,需在cmd交互器里执行
if real_username == username and real_passworld == passworld :
    print("Wlecome user {name} login...".format(name=username))
else:
    print("Out!")

7、while 循环:

count = 0
while True:
    print("count:",count)
    count = count+1 #count +=1

8、猜数游戏-while:

age_of_oldboy = 56
count = 0
while count<3:
    age = int(input("guess age:"))
    if age == age_of_oldboy:
        print("yes,you got it")
        break #退出本次循环
    elif age > age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
    count = count+1
else:
    print("you have tried too many times..fuck off")

9、for 循环:

for i in range(10):#内建函数range() ,它产生等差级数序列
    print("loop:",i)
    

for i in range(0,10,2):#打印从0 开始打印到10,跳一个打一个;guess for.py2代表步长,默认是1
    print("loop:",i)

for i in range(0,10,):
    if i<5:
        print("loop:",i)
    else:
        continue#跳出本次循环,进入下一循环
    print("hehe")

for i in range(10):
    print("-----------",i)
    for j in range(10):
        print(j)
        if j>5:
            break

10、猜数-for:

age_of_oldboy = 56
count = 0
for i in range(3):
    age = int(input("guess age:"))
    if age == age_of_oldboy:
        print("yes,you got it")
        break #退出本次循环
    elif age > age_of_oldboy:
        print("think smaller")
    else:
        print("think bigger")
else:
    print("Your account has been locked")


python学习(day1)

原文:http://12594527.blog.51cto.com/12584527/1901864

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!