码迷,mamicode.com
首页 > 编程语言 > 详细

python小白学习之路--01

时间:2018-03-29 19:02:35      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:continue   while True   for   count   

为了坚持而坚持(这话说了自己不信)
作为一个不懂编程的桌面,在技术的路上越走越远,严重到了找工作都很难的阶段,很心酸。。。作为一个干啥啥不行,吃啥啥有够,韩剧看不够,年纪又不小的我来说,在进步很难,不知路又走到哪就跑偏了,为了找到好工作而学习,至少希望可以升级做个小运维也是很满足的。
没有野心的人,想在现在生存,比有野心的人还要艰难。知足没有常乐,知足只是在后退而已!
网上学习python 对于我这样没组织没纪律的人,貌似起不到神马作用,忽略我没钱报培训班,还是硬着头皮,希望靠着大神的视频让我有所进步吧!
视频中python 入门第一篇结束后,要求写博客,我是假装听老师的话,然后过来自我安慰。
没啥语言功底,数学一般逻辑性不强,字迹潦潦草草,写出的东西也只有自己能看。
python01
小白入门的第一天
了解python,
神马是python,是蟒蛇。
为啥python要用.py 结尾,为了让其他人知道你写的这是个python 脚本,老师推荐最好使用pycharm脚本编辑器,下了个试用版,免费使用30天。
python 版本 3.6 (最好学习使用3.0 版本,2.0 过时了)

字符编码
ASCII 编码

  1. 输出“Hello world” (这是个仪式)
    变量名要求:a, 不能是数字开头 b,除了“_”以外不能包含其他特殊字符 c,包含大小写,数字,下划线 d,变量名要简单易懂表达所代表的东西 e,关键字不能成为变量名(and ,as,assert break class continue def del elif ………..)

定义变量 name = "Hello World"
print (name)

2.注释多行:(‘‘‘ ‘‘‘)
‘‘‘
多行注释,也可以打印多行
‘‘‘
打印多行:
例:
name = ‘alex‘
msg=‘‘‘
print ("my name is :" , {_name} )
name = "paoche ge"
‘‘‘.format(_name=name)
print (msg)
输出结果:
print ("my name is :" , alex )
name = "paoche ge"

3.用户输入(input)和格式化输出
例:用户输入(input)
username = input("type your username:")
print(username)
格式化输出
a,例:字符串拼接(‘‘‘ + ‘‘‘)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info1=‘‘‘
------info1 is ‘‘‘ + username ‘‘‘-----------
username is : ‘‘‘+ username ‘‘‘
age is : ‘‘‘ +age ‘‘‘
job is : ‘‘‘+job ‘‘‘
salary is : ‘‘‘ + salary
print (info1)

b, 字符串拼接(‘‘‘ %s ‘‘‘) ,s= string 字符串
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info2=‘‘‘
------info2 is %s-----------
username is : %s
age is : %s
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info2)

c, 字符串拼接(‘‘‘ %s ‘‘‘) s= string %d=整数 int=强制为数字?
username = input ("username is :")
age = int(input("age is :"))
job = input("job is:")
salary=input("salary is:")
info3=‘‘‘
------info3 is %s-----------
username is : %s
age is : %d
job is : %s
salary is : %s
‘‘‘%(username,username,age,job,salary)
print (info3)

d, 字符串拼接(使用排序.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info4=‘‘‘
------info4 is {0}-----------
username is : {0}
age is : {1}
job is : {2}
salary is : {3}
‘‘‘.format(username,age,job,salary)
print (info4)

e, 字符串拼接(.format)
username = input ("username is :")
age = input("age is :")
job = input("job is:")
salary=input("salary is:")
info5=‘‘‘
------info5 is {_username}-----------
username is : {_username}
age is : {_age}
job is : {_job}
salary is : {_salary}
‘‘‘.format(username=_username,
age=_age,
job=_job,
salary=_salary)
print (info5)

以上的输出(print)结果是:

**/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/user input.py"
username is :alex
your job is:IT
your age is:23
salary is :232323


--------info 5 alex------
yourname is :alex
your job is :IT
your age is :23
your salary is :232323

**

4.密码加密
例:
username = input("username:")
password = getpass.getpass("password:")
print (username,password)


5.if 条件判断语句()
例:
oldboy_age=55
age = input("guess oldboy age is :")

if oldboy_age == age
print("yes,you got it")
else
print("wrong ansser")

例:使用and 或是 or 连接符,满足条件才执行
username = ‘alex‘(错误的写法为 usename=alex ,表示是变量)
password = ‘123456‘
_username =input("please input your name:")
_pass=input("please input your password:")
if usename=_username and _pass=password:
print ("welcom login system")
else:
print("wrong user")

  1. while 循环语句
    while True 循环语句:表示为真则执行,反之则执行else 语句
    例:
    count = 0
    while True:
    print ("count:",count)
    count = count +1 # count +=1 每次循环加1,打印显示循环后的数值

while True 和 if 一起循环判断语句
例:
age= 55
while True:
guess = int(input("guess age :"))
if guess == age:
print("yes,you got it.")
elif guess < age:
print("think older.")
else:
print ("think smaler")
输入结果如下
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循环.py"
guess age :34
think older.
guess age :56
think smaler
guess age :

加上条件:使用户最多可以猜错三次条件
或是while 加条件语句如(while count < 3 )
定义计数器 count = 0, count += 1 每次循环+1
跳出循环break
例:
age= 55
count = 0
while True:
if count == 3:
break
guess = int(input("guess age :"))
if guess == age:
print("yes,you got it.")
elif guess < age:
print("think older.")
else:
print ("think smaler")
count += 1
执行的结果为:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循环.py"
guess age :23
think older.
guess age :23
think older.
guess age :23
think older.

Process finished with exit code 0


对以上的代码做优化
例:
age = 55
count = 0
while count < 3:
guess = int(input("guess age: "))
if age == guess:
print ("yes ,you got it")
elif age < guess:
print ("think older")
else:
print ("think smaller")
count += 1

** # if count == 3:

print ("you have try too many times.")**

else:
print("you have try too many times.")
输出结果是:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/whiel True 循环.py"
guess age: 34
think smaller
guess age: 35
think smaller
guess age: 78
think older
you have try too many times.

Process finished with exit code 0


6.for 循环
a,#最基本的for 循环
for i in range(10):
print(i)
输出结果:
0
1
2
3
4
5
6
7
8
9

b,显示不连续的数字(0:从0 开始,10:循环10次,2:每隔2位输出一个数字)
for i in range(0,10,2):
print (i)
输出结果为:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/for 循环.py"
0
2
4
6
8
Process finished with exit code 0
c,使用for 循环 猜年龄
例:
age = 55
for i in range (3):
guess = int(input("guess age:"))
if guess == age:
print ("yes,you got it .")
break
elif guess < age:
print ("think older")
else:
print ("think smaller")
else:
print("you have trid too many times.")
输出结果:
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 "/Users/a1/PycharmProjects/untitled3/day1/for 循环.py"
guess age:34
think older
guess age:34
think older
guess age:34
think older
you have trid too many times.
Process finished with exit code 0

6.continue #跳出本次循环,执行下一次循环
while True:
for i in range(3):
user = int(input("guess age:"))
if user == age:
print("1")
elif user > age:
print(">")
elif user < age:
print("<")
con = input("do you want con?")

if con == ‘c‘:
    continue
else:
    break

age_of_oldboy = 55
count = 0
while count < 3:
age = int(input("please input age of oldboy:"))

if age_of_oldboy == age:
    print("yes ,you got it!")
    break
elif age_of_oldboy < age:
    print("think smaller")
else:
    print("think older")
    count += 1

if count == 3:
    continue_confirm = input("do you want to keep guessing..?")
    if continue_confirm  != ‘n‘:
        count = 0
        # != 不等于

python小白学习之路--01

标签:continue   while True   for   count   

原文地址:http://blog.51cto.com/2926352/2092616

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!