python_day1

2019-04-11 10:18:16来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

hello word

# -*- coding:utf-8 -*-
#python 定义字符码为utf-8
# Author:Alex li

name="你好世界" \

print(name)#用python输出你好世界
1 # -*- coding:utf-8 -*-
2 #python2 定义字符码
3 # Author:Alex li
4 
5 name="你好世界" \
6 
7 print(name)

 


war


#Author:Alex liname="Alex li"msg='''
name2=name
print("My name is ",name,name2)

name="PaoChe Ge"#多行输出
'''
#gf_of_oldboy="chen rong hua"
#GfOfOldboy="chen rong hua"#变量只能是字母,数字或下划线的组合,第一个字符不能是关键字,(也可以是中文,但不要这么干),变量名不能为关键字

print(msg)

#PIE=#用大写定义常量(可以更改,但不应该改)检查常量在世设置就抛出异常,常量不是大写字母就抛出异常

#print(name,name2)
 1 # Author:Alex li
 2 
 3 name="Alex li"
 4 msg='''
 5 name2=name
 6 print("My name is ",name,name2)
 7 
 8 name="PaoChe Ge"
 9  '''
10 #gf_of_oldboy="chen rong hua"
11 #GfOfOldboy="chen rong hua"
12 
13 print(msg)
14 
15 #PIE=
16 
17 #print(name,name2)

 


interaction
# Author:Alex li
name=input("name:")#输入名字
#raw_input("name:") 2.x input 3.x#python2.x和3.x的输入区别
#input 2.x=#2.x中别用这个输入
age=int(input("age:")) #intger和int一样,将输入的字符转换为数字型
print(type(age)) #输出age的类型
#print(type(str(age))转为整型再输出
job=input("job:")
salary=input("salary:")

info='''
-------- info of %s -------

Name:%s
Age:%d #%d表述数字型,输入若为整型就会报错
Job:%s
Salary:%s

''' %(name,name,age,job,salary)#多行输出,%s,%d是占位符

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(info3)

 1 # Author:Alex li
 2 
 3 
 4 name=input("name:")
 5 #raw_input("name:") 2.x   input 3.x
 6 #input 2.x=
 7 age=int(input("age:")) #intger
 8 print(type(age)) #,type(str(age)))
 9 job=input("job:")
10 salary=input("salary:")
11 
12 info='''
13 -------- info of  %s -------
14 
15 Name:%s
16 Age:%d
17 Job:%s
18 Salary:%s
19 
20 ''' %(name,name,age,job,salary)
21 
22 info2='''
23 -------- info of  {_name} -------
24 
25 Name:{_name}
26 Age:{_age}
27 Job:{_job}
28 Salary:{_salary}
29 
30 '''.format(_name=name,
31            _age=age,
32            _job=job,
33            _salary=salary)
34 
35 info3='''
36 -------- info of  {0} -------
37 
38 Name:{0}
39 Age:{1}
40 Job:{2}
41 Salary:{3}
42 
43 '''.format(name,age,job,salary)
44 print(info3)

passwd

 1 # -*- coding:utf-8 -*-
 2 #  Author:Alex li
 3 import getpass
 4 
 5 _username='alex'
 6 _password='abc123'
 7 
 8 username = input("username:")
 9 #password = getpass.getpass("passworf:") 隐藏输入
10 password = input("passworf:")
11 
12 if _username==username and _password==password:
13     print("welcome user {name} login..".format(name=username))
14 else:
15     print("Invalid username or password!")#if else语句

guess

 

 1 # Author:Alex li
 2 
 3 
 4 age_of_oldboy = 56
 5 
 6 count = 0
 7 #while True:
 8 while count < 3:
 9     #if count == 3:
10     #    break
11     guess_age =int( input("guess age:"))
12     if guess_age == age_of_oldboy :
13         print("yes,you got it.")
14         break
15     elif guess_age > age_of_oldboy:
16         print("think smaller...")
17     else:
18         print("think bigger")
19     count +=1
20 else:
21     print("you have tried too mant times..fuck off")

 

guess for

# Author:Alex li


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("think bigger")
else:
      print("you have tried too mant times..fuck off")

guess任性玩#19-22行加入了循环判断清零

 1 # Author:Alex li# Author:Alex li
 2 
 3 
 4 age_of_oldboy = 56
 5 
 6 count = 0
 7 
 8 while count < 3:
 9 
10     guess_age =int( input("guess age:"))
11     if guess_age == age_of_oldboy :
12         print("yes,you got it.")
13         break
14     elif guess_age > age_of_oldboy:
15         print("think smaller...")
16     else:
17         print("think bigger")
18     count +=1
19     if count ==3:
20         countine_confirm=input("do you want to keep guessing..?")
21         if countine_confirm !='n':
22             count=0
23 else:
24     print("you have tried too mant times..fuck off")

while和循环套循环

 1 # -*- coding:utf-8 -*-
 2 # Author:Alex li
 3 
 4 '''
 5 count = 0
 6 while True: #循环
 7     print("count:",count)
 8     count = count +1  #count +=1
 9     if count == 1000:
10         break   #破坏循环
11 
12 
13 
14 
15 for i in range(10):# 循环10次,每次i从range(10)中取一个值
16     print("loop",i)
17 
18 
19 for i in range(0,10,2):#(0-10,每隔一个打一次)
20     print("loop",i)
21 
22 
23 
24 for i in range(0,10):
25     if i<5:
26          print("loop",i)
27 
28     else:
29         continue #跳出本次循环,继续到下次循环
30 
31     print("hehe...")
32 '''
33 
34 
35 for i in range (10):
36     print('----------',i)
37     for j in range (10):
38         print(j)
39         if j>5:
40             break#跳出到大循环  continue结束本次

转义字符:https://www.jianshu.com/p/a5fcb58d3375

for,while,if语句:https://blog.csdn.net/qq_39407518/article/details/79822498

 


原文链接:https://www.cnblogs.com/lybccc/p/10627998.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Hello Flask

下一篇:别开心太早,Python 官方文档的翻译差远了