本节内容:

  1.pycharm工具的使用

  2.进制运算

  3.表达式if ...else语句

  4.表达式for 循环

  5.break and continue

  6.表达式while 循环

1.pycharm工具的使用

【下载安装】

下载:http://pan.baidu.com/s/1jHPbo5g 密码:yhhf

破解:pycharm有两个版本,社区版和专业版,建议购买专业版~~~~

安装:下一步下一步,比较简单,就不废话了。

视频:http://list.youku.com/albumlist/show?id=28961509&ascending=1&page=1&qq-pf-to=pcqq.group

使用

1. 养成一个好习惯,一个项目里面建立相关的文件夹和脚本

2.优化设置

  • 更改脚本里面内容的字体大小:File>Settings>Ediror>Colors & Fonts>Font
  • 设置脚本的抬头注释:File>Settings>Editor>File and Code Templates>Python Script
  • 设置显示脚本的行数:File>Setting>Editor>General>Apperance>勾选“Show line numbers”。
  • pycharm Tab键设置成4个空格:File>Setting>Editor>Code Style>python

3.效果演示

2.进制运算

【补充知识:变量】

python中的变量是指向内存单元的值。

 name = "alex"
name2 = name print(name,name2) name = "jack" print(name,name2)
alex alex
jack alex

【%s,%d,%f,%r】

%s 任意字符
%d 必须是整形数字
%f 浮点型
%r raw string 原生字符,不转译

 msg = "my name is %s and age is %s" %("lijun","")
print(msg)
msg = "my name is %s and age is %d" %("lijun",22)
print(msg)
msg = "my name is %s and age is %f" %("lijun",22)
print(msg)
msg = "my name is %s and age is %r" %("lijun","aaa\n\tbbb")
print(msg)
msg = "my name is %s and age is %s" %("lijun","aaa\n\tbbb")
print(msg)
my name is lijun and age is 22
my name is lijun and age is 22
my name is lijun and age is 22.000000
my name is lijun and age is 'aaa\n\tbbb'
my name is lijun and age is aaa
bbb

【算数运算】

【比较运算】

【赋值运算】

【逻辑运算】

【成员运算】

【位运算】

a = 22
o = 56 128 64 32 16 8 4 2 1
a = 0 0 0 1 0 1 1 0
o = 0 0 1 1 1 0 0 0 a&o = 0 0 0 1 0 0 0 0 = 32
a|o = 0 0 1 1 1 1 1 0 = 62
a<<1 = 0 0 1 0 1 1 0 0 = 44
o>>1 = 0 0 0 1 1 1 0 0 = 28

【运算符优先级】

3.表达式if ...else语句

案例1:用户登录验证
 # 提示输入用户名和密码

 # 验证用户名和密码
# 如果错误,则输出用户名或密码错误
# 如果输入的用户名是chenlijun,就显示禁止登陆
# 如果成功,则输出 成功
# 最多允许输错三次 login = "root"
passwd = "root123" for i in range(3):
newlogin = input("please input your user:")
newpasswd = input("please input youer passwd:")
if login == newlogin and passwd == newpasswd:
print ("login successfyl")
break
elif newlogin == 'chenlijun':
print ("use %s is disable" % newlogin)
break
else:
print ("user or passwd error") else:
print ("你的密码输错三次以上,被锁到海枯石烂,哈哈哈!You input the wrong password three times, locked to the end, ha ha ha!")

案例2:猜年龄游戏

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了

 #!/usr/bin/python
# -*- coding: UTF-8 -*-
age = 24 newage = int(input("youerage:")) if age == newage:
print ("恭喜你,猜对啦~~") elif age < newage:
print ("我有那么老吗?臭傻逼~~~") else:
print ("我有那么小吗?臭傻逼~~~")

4.表达式for 循环

【循环原理其实就是一个列表】

案例1:年龄输错三次就退出,结束,猜小了提示年龄小,猜大了提示年龄大。


 #!/usr/bin/python
# -*- coding: UTF-8 -*-
#blog:http://www.cnblogs.com/linux-chenyang/
age = 24 for i in range(10): if i == 3:
print("bay....")
break
newage = int(input("youerage:")) if age == newage:
print ("恭喜你,猜对啦~~")
break elif age < newage:
print ("我有那么老吗?臭傻逼~~~") else:
print ("我有那么小吗?臭傻逼~~~")

youerage:23
我有那么小吗?臭傻逼~~~
youerage:47
我有那么老吗?臭傻逼~~~
youerage:100
我有那么老吗?臭傻逼~~~
bay....

案例2:增加提示,如果输入三次还想继续猜,输入Y,输入N退出。

 #!/usr/bin/python
# -*- coding: UTF-8 -*-
#blog:http://www.cnblogs.com/linux-chenyang/ age = 24
count = 0 for i in range(10): if count == 3:
prompt = input("do you want to continue(Y/N):")
if prompt == "Y":
count = 0
else:
break
newage = int(input("youerage:")) if age == newage:
print ("恭喜你,猜对啦~~")
break elif age < newage:
print ("我有那么老吗?臭傻逼~~~") else:
print ("我有那么小吗?臭傻逼~~~") count += 1
youerage:22
我有那么小吗?臭傻逼~~~
youerage:29
我有那么老吗?臭傻逼~~~
youerage:30
我有那么老吗?臭傻逼~~~
do you want to continue(Y/N):Y
youerage:22
我有那么小吗?臭傻逼~~~
youerage:22
我有那么小吗?臭傻逼~~~
youerage:22
我有那么小吗?臭傻逼~~~
do you want to continue(Y/N):N Process finished with exit code 0

5.break and continue

案例1:循环套循环

 #!/usr/bin/python
# -*- coding: UTF-8 -*-
#blog:http://www.cnblogs.com/linux-chenyang/ for i in range(10):
if i == 5:
for j in range(10):
print("inner forloop", j)
if j == 8:
break
continue
print("forloop", i)
forloop 0
forloop 1
forloop 2
forloop 3
forloop 4
inner forloop 0
inner forloop 1
inner forloop 2
inner forloop 3
inner forloop 4
inner forloop 5
inner forloop 6
inner forloop 7
inner forloop 8
forloop 6
forloop 7
forloop 8
forloop 9 Process finished with exit code 0

6.表达式while 循环

案例1:两种方法对比时间的快慢
 #!/usr/bin/python
# -*- coding: UTF-8 -*-
#blog:http://www.cnblogs.com/linux-chenyang/ import time                      #导入时间模块
t0_start = time.time()                #time.time()表示当前时间 count=0
while True:
if count == 1000000:
break
# print ("计数开始:", count)
count += 1
print ("count:",time.time()-t0_start,count) t_start = time.time()
count0=0
while count0 < 1000000:
count0 += 1
print ("count0:",time.time()-t_start,count0)

count: 0.488048791885376 1000000
count0: 0.4170417785644531 1000000 Process finished with exit code 0

案例2:还是猜年龄,猜错三次就提示笨蛋,并退出。

 age = 24
count = 0 while count < 3: newage = int(input("youerage:")) if age == newage:
print ("恭喜你,猜对啦~~") elif age < newage:
print ("我有那么老吗?臭傻逼~~~") else:
print ("我有那么小吗?臭傻逼~~~") count += 1
else:
print("笨死了,猜那么多次还是猜不对~")
youerage:1
我有那么小吗?臭傻逼~~~
youerage:2
我有那么小吗?臭傻逼~~~
youerage:3
我有那么小吗?臭傻逼~~~
笨死了,猜那么多次还是猜不对~ Process finished with exit code 0

7.知识补充

【三元运算】

result = 1 if 条件 else 2

如果条件为真:result = 值1
如果条件为假:result = 值2
 
 
 

												

Python_oldboy_自动化运维之路(二)的更多相关文章

  1. Python_oldboy_自动化运维之路_paramiko,mysql(十二)

    本节内容: paramiko mysql 1.paramiko http://www.cnblogs.com/wupeiqi/articles/5095821.html paramiko是一个模块,s ...

  2. Python_oldboy_自动化运维之路_线程,进程,协程(十一)

    本节内容: 线程 进程 协程 IO多路复用 自定义异步非阻塞的框架 线程和进程的介绍: 举个例子,拿甄嬛传举列线程和进程的关系: 总结:1.工作最小单元是线程,进程说白了就是提供资源的 2.一个应用程 ...

  3. Python_oldboy_自动化运维之路_面向对象2(十)

    本节内容: 面向对象程序设计的由来 什么是面向对象的程序设计及为什么要有它 类和对象 继承与派生 多的态与多态性 封装 静态方法和类方法 面向对象的软件开发 反射 类的特殊成员方法 异常处理 1.面向 ...

  4. Python_oldboy_自动化运维之路(八)

    本节内容: 列表生成式,迭代器,生成器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器,生成器 1.列表生成式 #[列表生成] #1.列 ...

  5. Python_oldboy_自动化运维之路_函数,装饰器,模块,包(六)

    本节内容 上节内容回顾(函数) 装饰器 模块 包 1.上节内容回顾(函数) 函数 1.为什么要用函数? 使用函数之模块化程序设计,定义一个函数就相当于定义了一个工具,需要用的话直接拿过来调用.不使用模 ...

  6. Python_oldboy_自动化运维之路(三)

    本节内容 列表,元组,字典 字符串操作 copy的用法 文件操作 1.列表,元组,字典 [列表] 1.定义列表 names = ['Alex',"Tenglan",'Eric'] ...

  7. Python_oldboy_自动化运维之路(一)

    python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...

  8. Python_oldboy_自动化运维之路_socket编程(十)

    链接:http://www.cnblogs.com/linhaifeng/articles/6129246.html 1.osi七层 引子: 须知一个完整的计算机系统是由硬件.操作系统.应用软件三者组 ...

  9. Python_oldboy_自动化运维之路_面向对象(十)

    面向对象编程 OOP编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率 ,另外,基于面向 ...

随机推荐

  1. CSU1911 Card Game 【FWT】

    题目链接 CSU1911 题解 FWT模板题 #include<algorithm> #include<iostream> #include<cstdlib> #i ...

  2. 【bzoj4013】 HNOI2015—实验比较

    http://www.lydsy.com/JudgeOnline/problem.php?id=4013 (题目链接) 题意 给出$n$个数的$m$个大小关系,问它们之间可以形成的单调不降的序列有多少 ...

  3. 《Linux内核设计与实现》学习总结 Chap18

    一.准备开始 1.一个确定的bug,但大部分bug通常都不是行为可靠且定义明确的. 2.一个藏匿bug的内核版本. 3.相关内核代码的知识和运气. 二.内核中的bug 1.bug的表象: 明白无误的错 ...

  4. fzyzojP3372 -- [校内训练20171124]博弈问题

    对于每个点都要答案 还是异或 trie树合并石锤了 朴素枚举是O(n^2*17)的 怎么办呢? 我们发现合并的时候,一些部分的trie的子树还是不变的 改变的部分也就是合并的复杂度可以接受 鉴于大部分 ...

  5. React-Router 动画 Animation

    React-Router动画实际上和React动画没什么区别,都是使用 'react-addons-css-transition-group' 这个组件:但是,和普通的 React-Router 的 ...

  6. shiro权限认证与授权

    什么是shiro? Shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证,权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 为什么要用sh ...

  7. windows10下基于vs2015的 caffe安装教程及python接口实现

    啦啦啦:根据网上的教程前一天安装失败,第二天才安装成功.其实caffe的安装并不难,只是网上的教程不是很全面,自己写一个,留作纪念. 准备工作 Windows10 操作系统 vs2015(c++编译器 ...

  8. python安装包提示error: option --single-version-externally-managed not recognized

    pip install mysql-connector-python-rf==2.2.2 安装包的时候提示错误信息:error:option--single-version-externally-ma ...

  9. HTTP 错误 500.19 请求的页面的相关配置数据无效 解决办法

    "HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法   HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该 ...

  10. VsCode搭建Java开发环境(Spring Boot项目创建、运行、调试)

    源码地址:https://github.com/YANGKANG01/Spring-Boot-Demo 安装扩展 安装如下两个主要扩展即可,这两个扩展已关联java项目开发主要使用的maven.spr ...