设为首页 收藏本站
查看: 865|回复: 0

[经验分享] Python学习笔记——day1

[复制链接]

尚未签到

发表于 2018-8-10 08:57:40 | 显示全部楼层 |阅读模式
  #Python day1
  标签: python
  [TOC]
  ##1.Hellow World程序
  ###1)Hellow World
  在linux里创建一个叫hellow.py的文件。

  文件内容:
  

#!/usr/bin/env python  
print("Hellow World!")
  

  在linux中需要指定解释器#!/usr/bin/env python
  意思是到整个linux系统中找名为python的环境变量。

  给予文件执行权限,使用./hellow.py执行文件。

  输出结果:
  

Hellow World!  

  Python常用转义符:


转义字符
具体描述
\n
换行
\r
回车
\v
纵向制表符
\t
横向制表符
\"
"
\\
\
\(在行尾时)
续行符
\a
响铃
\b
退格(Backspace)
\000
空  <br />
  <br />
  <br />
  <br />
  ##2.变量
  ###1)什么是变量?

  变量用于存储在计算机程序中引用和操作的信息。它们还提供了一种用描述性名称标记数据的方法,这样我们的程序就能更清晰地被读者和我们自己理解。将变量看作保存信息的容器是很有帮助的。它们的唯一目的是在内存中标记和存储数据。然后可以在整个程序中使用这些数据。

  ###变量定义的规则:


  • 变量定义的规则

    • 变量名只能是字母、数字或下划线的任意组合
    • 变量名的第一个字符不能是数字
    • 写的变量名要有含义,否则自己和别人都看不懂
    • 变量名最好不要是中文或拼音,多个单词用下划线分隔
    • 以下关键字不能声明为变量名  

      ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']

  ###什么是常量?

  常量:定义后,不会变的量,比如π是3.14...
  python定义常量,变量名要大写比如:PIE = "em"

  <br />
  ###2)变量的赋值:
  

name = "byh"  
name2 = name
  

  
print(name,"-----",name2)
  

  
name = "em"
  
print(name,"-----",name2)
  

  print:输出字符

  <br/>

  输出结果:
  

  
byh ----- byh
  
em ----- byh
  

  Process finished with exit code 0
  

  
>###为什么第二个name2输出的是 byh,而不是em呢?
  
* 因为:
  - name2只是通过name找到byh的,name2≠name
  - name = em只是从新定义了name,name2还是通过第一个name变量找到byh
  

  
<br/>
  
###注释:
  

  
当行注释|“#”标识当行
  
---------|---------------
  
多行注释|''' 或 """  都表示多行注释
  

  
###3)三元运算
  

  a = 1
  b = 5
  c = 9
  d = a if a > b else c
  print(d)
  d = a if a < b else c
  print(d)
  

  
输出结果:
  

  9        #如果a > b条件不成立,结果就是c的值
  1        #如果a < b条件成立,结果就是a的值
  

  
<br>
  
<br>
  

  
---
  

  
<br>
  
<br>
  

  
##3.用户输入
  
###1)简单用户输入
  

  username = input(&quot;username:&quot;)
  password = input(&quot;password:&quot;)
  print(&quot;Hellow&quot;,username)
  

  
>输入密码时,如果要密码不可见,需要使用`getpass`模块中的getpass方法:
  

  import getpass
  username = input(&quot;username:&quot;)
  password = getpass.getpass(&quot;password:&quot;)
  print(&quot;Hellow&quot;,username)
  

<br />  

  
###2)用户输入,输出调用变量
  

  
>方法一:
  

  name = input(&quot;name:&quot;)
  age = int(input(&quot;age:&quot;))
  print(type(age))
  job = input(&quot;job:&quot;)
  salary = int(input(&quot;salary:&quot;))
  print(type(salary))
  info = '''
  -------- info of %s--------
  Name:%s
  Age:%d
  Job:%s
  Salary:%d
  ''' %(name,name,age,job,salary)
  print(info)
  

  
%s|代表string字符串
  
---|---
  
%d|代表整数
  
%f|代表有小数点的数
  
int()|强制转换类型为integer
  
str()|强制转换类型为string
  
print(type(salary))|输出字符串类型
  

  
>输出结果:
  

  name:em
  age:19
  <class 'int'>
  job:IT
  salary:1234567
  <class 'int'>
  -------- info of em--------
  Name:em
  Age:19
  Job:IT
  Salary:1234567
  Process finished with exit code 0
  

<br/>  
>方法二:
  

  #Author:byh
  name = input(&quot;name:&quot;)
  age = int(input(&quot;age:&quot;))
  print(type(age))
  job = input(&quot;job:&quot;)
  salary = int(input(&quot;salary:&quot;))
  print(type(salary))
  info = '''
  -------- info of {_name}--------
  Name:{_name}
  Age:{_age}
  Job:{_job}
  Salary:{_salary}
  ''' .format(_name=name,
  _age=age,
  _job=job,
  _salary=salary)
  print(info)
  

  
>输出结果:
  

  name:em
  age:19
  <class 'int'>
  job:IT
  salary:1234567
  <class 'int'>
  -------- info of em--------
  Name:em
  Age:19
  Job:IT
  Salary:1234567
  Process finished with exit code 0
  

  
<br/>
  

  
>方法三:
  

  #Author:byh
  name = input(&quot;name:&quot;)
  age = int(input(&quot;age:&quot;))
  print(type(age))
  job = input(&quot;job:&quot;)
  salary = int(input(&quot;salary:&quot;))
  print(type(salary))
  info = '''
  -------- info of {0}--------
  Name:{0}
  Age:{1}
  Job:{2}
  Salary:{0}
  ''' .format(name,name,age,job,salary)
  print(info)
  

  
>输出结果:
  

  name:em
  age:19
  <class 'int'>
  job:IT
  salary:1234567
  <class 'int'>
  -------- info of em--------
  Name:em
  Age:em
  Job:19
  Salary:em
  Process finished with exit code 0
  

  
<br/>
  
<br/>
  

  
---
  

  
<br/>
  
<br/>
  

  
##4.表达式if ... else
  
###场景一 : 用户登陆验证
  

  _username = &quot;byh&quot;
  _password = &quot;123&quot;
  username = input(&quot;username:&quot;)
  password = input(&quot;password:&quot;)
  if _username == username and _password == password:
  print(&quot;Welcome user {name} login...&quot;.format(name=_username))
  else:
  print(&quot;Invaild username or password!&quot;)
  

elif|如果这个条件不成立,那么下个条件是否成立呢  
---|---
  

  
>输出结果:
  

  #验证成功输出
  username:byh
  password:123
  Welcome user byh login...
  #验证失败输出
  username:em
  password:123
  Invalid username or password!
  

###场景二 : 猜年龄游戏  

  _myage = 22
  myage = int(input(&quot;myage:&quot;))
  if myage == _myage:
  print(&quot;yes, it is&quot;)
  elif myage > _myage:
  print(&quot;should be smaller..&quot;)
  else:
  print(&quot;should be more big..&quot;)
  

  
>输出结果
  

  myage:17
  should be more big..
  myage:23
  should be smaller..
  myage:22
  yes, it is
  

<br/>  
<br/>
  

  
---
  

  
<br/>
  
<br/>
  

  
##5.while循环
  
###1)简单while循环
  

  count = 0
  while True:
  print(&quot;count:&quot;,count)
  count +=1
  if count == 1000:
  break
  

>当这个条件成立时:True(永远为真),如果没有定义 `break`条件结束循环,则会一会循环下去。  

  
<br/>
  

  
###2)while循环猜年龄游戏
  

  #实现用户可以不断的猜年龄,最多3次机会,继续按如意键,退出按“n”
  _myage = 22
  count = 0
  while count < 3:
  myage = int(input(&quot;myage:&quot;))
  if myage == _myage:
  print(&quot;yes it is.&quot;)
  break
  elif myage > _myage:
  print(&quot;should be smaller...&quot;)
  else:
  print(&quot;should be more big...&quot;)
  count +=1
  if count == 3:
  countine_config = input(&quot;do you want to keep gussing?&quot;)
  if countine_config !=&quot;n&quot;:
  count = 0
  

  
break|结束当前整个循序
  
---|---
  
continue|跳出本次循环,进入下次循环
  
count|添加计数
  

  
>输出结果:
  

  #继续猜:
  myage:1
  should be more big...
  myage:2
  should be more big...
  myage:3
  should be more big...
  do you want to keep gussing?
  myage:12
  should be more big...
  myage:22
  yes it is.
  Process finished with exit code 0
  #不猜退出:
  myage:1
  should be more big...
  myage:2
  should be more big...
  myage:3
  should be more big...
  do you want to keep gussing?n
  Process finished with exit code 0
  

<br/>  
<br/>
  

  
---
  

  
<br/>
  
<br/>
  

  
##6.for循环
  
###1)简单for循环
  

  #0,10是范围,1是增数
  for i in range(0,10,1):
  print(&quot;number&quot;,i)
  

>输出结果:  

  number 0
  number 1
  number 2
  number 3
  number 4
  number 5
  number 6
  number 7
  number 8
  number 9
  Process finished with exit code 0
  

  
###2)for循环猜年龄游戏
  

  #用户最多猜3次
  _myage = 22
  for i in range(3):
  myage = int(input(&quot;myage:&quot;))
  if myage == _myage:
  print(&quot;yes it is.&quot;)
  break
  elif myage > _myage:
  print(&quot;should be smaller..&quot;)
  else:
  print(&quot;should be more big..&quot;)
  else:
  print(&quot;you have tried too many times&quot;)
  

  
>输出结果:
  

  myage:1
  should be more big..
  myage:2
  should be more big..
  myage:3
  should be more big..
  you have tried too many times
  Process finished with exit code 0
  

  
##7.作业
  
###1)作业三:多级菜单
  
* 三级菜单
  
* 可一次选择进入各子菜单
  
* 使用知识点:列表、字典
  

  

  #Author:byh
  #定义三级字典
  data = {
  '北京':{
  &quot;昌平&quot;:{
  &quot;沙河&quot;:[&quot;oldboy&quot;,&quot;test&quot;],
  &quot;天通苑&quot;:[&quot;宜家&quot;,&quot;大润发&quot;],
  },
  &quot;朝阳&quot;:{},
  &quot;海淀&quot;:{},
  },
  '山东':{
  &quot;德州&quot;:{},
  &quot;青岛&quot;:{},
  &quot;济南&quot;:{},
  },
  '广东':{
  &quot;东莞&quot;:{},
  &quot;广州&quot;:{},
  &quot;佛山&quot;:{},
  },
  }
  exit_flag = False
  while not exit_flag:
  for i in data:      #打印第一层
  print(i)
  

choice = input("选择进入1:")  
if choice in data:  #判断输入的层在不在
  while not exit_flag:
  for i2 in data[choice]:  #打印第二层
  print(i2)
  

  choice2 = input("选择进入2:")
  if choice2 in data[choice]:     #判断输入的层在不在
  while not exit_flag:
  for i3 in data[choice][choice2]:    #打印第三层
  print(i3)
  

  choice3 = input("选择进入3:")
  if choice3 in data[choice][choice2]:    #判断输入的层在不在
  for i4 in data[choice][choice2][choice3]:   #打印最后一层
  print(i4)
  

  choice4 = input("最后一层,按b返回:")
  if choice4 == "b":  #返回
  pass  #占位符,或以后可能添加代码
  elif choice4 == "q":    #退出
  exit_flag = True
  if choice3 == "b":
  break
  elif choice3 == "q":
  exit_flag = True
  if choice2 == "b":
  break
  elif choice2 == "q":
  exit_flag = True
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549471-1-1.html 上篇帖子: python博客园示例,重点使用装饰器 下篇帖子: python列表的增删改查
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表