Python_oldboy_自动化运维之路(二)
本节内容:
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 = 值2

Python_oldboy_自动化运维之路(二)的更多相关文章
- Python_oldboy_自动化运维之路_paramiko,mysql(十二)
本节内容: paramiko mysql 1.paramiko http://www.cnblogs.com/wupeiqi/articles/5095821.html paramiko是一个模块,s ...
- Python_oldboy_自动化运维之路_线程,进程,协程(十一)
本节内容: 线程 进程 协程 IO多路复用 自定义异步非阻塞的框架 线程和进程的介绍: 举个例子,拿甄嬛传举列线程和进程的关系: 总结:1.工作最小单元是线程,进程说白了就是提供资源的 2.一个应用程 ...
- Python_oldboy_自动化运维之路_面向对象2(十)
本节内容: 面向对象程序设计的由来 什么是面向对象的程序设计及为什么要有它 类和对象 继承与派生 多的态与多态性 封装 静态方法和类方法 面向对象的软件开发 反射 类的特殊成员方法 异常处理 1.面向 ...
- Python_oldboy_自动化运维之路(八)
本节内容: 列表生成式,迭代器,生成器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器,生成器 1.列表生成式 #[列表生成] #1.列 ...
- Python_oldboy_自动化运维之路_函数,装饰器,模块,包(六)
本节内容 上节内容回顾(函数) 装饰器 模块 包 1.上节内容回顾(函数) 函数 1.为什么要用函数? 使用函数之模块化程序设计,定义一个函数就相当于定义了一个工具,需要用的话直接拿过来调用.不使用模 ...
- Python_oldboy_自动化运维之路(三)
本节内容 列表,元组,字典 字符串操作 copy的用法 文件操作 1.列表,元组,字典 [列表] 1.定义列表 names = ['Alex',"Tenglan",'Eric'] ...
- Python_oldboy_自动化运维之路(一)
python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...
- Python_oldboy_自动化运维之路_socket编程(十)
链接:http://www.cnblogs.com/linhaifeng/articles/6129246.html 1.osi七层 引子: 须知一个完整的计算机系统是由硬件.操作系统.应用软件三者组 ...
- Python_oldboy_自动化运维之路_面向对象(十)
面向对象编程 OOP编程是利用“类”和“对象”来创建各种模型来实现对真实世界的描述,使用面向对象编程的原因一方面是因为它可以使程序的维护和扩展变得更简单,并且可以大大提高程序开发效率 ,另外,基于面向 ...
随机推荐
- BZOJ2436 [Noi2011]Noi嘉年华 【dp】
题目链接 BZOJ2436 题解 看这\(O(n^3)\)的数据范围,可以想到区间\(dp\) 发现同一个会场的活动可以重叠,所以暴力求出\(num[l][r]\)表示离散化后\([l,r]\)的完整 ...
- 《Linux内核设计与实现》第7章读书笔记
第七章 链接 一. 链接的概念 链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程.可以执行于编译.加载和运行时,由叫做链接器(可实现分离编译)的程序自动执行. 二.静态链接 为了创建静态 ...
- java类加载详解
1,类的加载过程: JVM将类加载过程分为三个步骤:装载(load),链接(link)和初始化(initialize),其中链接又分为三个步骤: 验证(varification),准备(Prepara ...
- 【数学】【P5150】 生日礼物
Description 给定 \(n\),求 \[\sum_{i}~\sum_j~[lcm(i,j)~=~n]\] input 一行一个整数代表 \(n\) Output 一行一个整数代表答案 Hin ...
- Linux crontab 命令格式与举例
每五分钟执行 */5 * * * * 每小时执行 0 * * * * 每天执行 0 0 * * * 每周执行 0 0 * * 0 每月执行 0 0 1 ...
- Python 爬虫入门(二)—— IP代理使用
上一节,大概讲述了Python 爬虫的编写流程, 从这节开始主要解决如何突破在爬取的过程中限制.比如,IP.JS.验证码等.这节主要讲利用IP代理突破. 1.关于代理 简单的说,代理就是换个身份.网络 ...
- bzoj 5015 [Snoi2017]礼物 矩阵乘法
5015: [Snoi2017]礼物 Time Limit: 15 Sec Memory Limit: 512 MBSubmit: 163 Solved: 115[Submit][Status][ ...
- 一、linux学习之centOS系统安装(VMware下安装)
一.下载 这个真的没有什么技术含量,也不附下载连接了.这里需要说明的是,其实在VMware下安装centOS是非常简单的,但是这里我要纪录的是在PC上安装centOS,之所以跟标题有出入是因为为了纪录 ...
- NATS_05:NATS服务器部署
1.NATS安装前的普及 NATS 的服务器是使用 GoLang 语言开发的,其可执行文件的名字为:gnatsd,表示:Go NATS Daemon.NATS服务器是一个开源软件,基于 MIT 许可证 ...
- 使用Githubdesktop管理Eclipse项目
使用Githubdesktop管理Eclipse项目 觉得有用的话,欢迎一起讨论相互学习~[Follow] 方案 使用Eclipse创建项目,使用githubdesktop进行管理 项目右键, Tea ...
