python-python基础1(变量、判断、循环、模块、数据运算)
一、变量
name=input("name:")
age=input("age:")
job=input("job:") info='''
--- information of %s ---
name:%s
age:%s
job:%s
''' % (name,name,age,job) info2='''
--- information2 of {name} ---
name:{name}
age:{age}
job:{job}
'''.format(name=name,age=age,job=job) print(info2)
二、if...else表达式 和 while循环
一直循环:while True
例1:
age_of_jehu=25 count=1
while count<=3:
age=int(input("guess_age:"))
if age==age_of_jehu:
print("Yes,you got it.")
break
elif age < age_of_jehu:
print("The number you input is smaller.")
else:
print("The number you input is bigger.")
count+=1
else:
print("You have tired too much times.")
例2:
age_of_jehu=25 count=0
while count<3:
age=int(input("guess_age:"))
if age==age_of_jehu:
print("Yes,you got it.")
break
elif age < age_of_jehu:
print("The number you input is smaller.")
else:
print("The number you input is bigger.")
count+=1
if count == 3:
continue_confirm=input("Do you want to continue this game? y or n\n")
if continue_confirm=="y":
count=0
else:
print("The game is over!")
三、for循环
for i in range(10):
if i<5:
continue #不往下走了,直接进入下一次loop
print("loop:", i )
编写登陆接口
- 输入用户名密码
- 认证成功后显示欢迎信息
- 输错三次后锁定
username="jehu"
password="" count=0
while count<5:
username1 = input("username:")
password1 = input("password:")
if username1==username and password1==password:
print("Congratulations, login successfully.")
break
else:
print("wrong username or password.")
continue1=input("Do you want to continue typing? y or n\n")
if continue1=="n":
break
count+=1
else:
print("You have made more than 5 mistakes.")
四、模块
Python的强大之处在于他有非常丰富和强大的标准库和第三方库,几乎你想实现的任何功能都有相应的Python库支持,以后的课程中会深入讲解常用到的各种库。
先简单认识sys和os这两个模块
import sys
import os #打印当前文件路径
print(sys.argv)
#调用系统命令
os.system("dir") #执行命令,不保存结果 os_read=os.popen("dir").read()
print(os_read)
五、数据运算
比较运算:
赋值运算:
逻辑运算:
成员运算:
身份运算:
位运算:
#!/usr/bin/python a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
c = 0 c = a & b; # 12 = 0000 1100
print "Line 1 - Value of c is ", c c = a | b; # 61 = 0011 1101
print "Line 2 - Value of c is ", c c = a ^ b; # 49 = 0011 0001 #相同为0,不同为1
print "Line 3 - Value of c is ", c c = ~a; # -61 = 1100 0011
print "Line 4 - Value of c is ", c c = a << 2; # 240 = 1111 0000
print "Line 5 - Value of c is ", c c = a >> 2; # 15 = 0000 1111
print "Line 6 - Value of c is ", c
*按位取反运算规则(按位取反再加1) 详解http://blog.csdn.net/wenxinwukui234/article/details/42119265
运算符优先级:
python-python基础1(变量、判断、循环、模块、数据运算)的更多相关文章
- Python编程基础[条件语句if 循环语句 for,while](二)
ython条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语句的执行过程: if 判断条件: 执行语句……else: 执行语句…… x= ...
- Python入门基础之变量和数据类型
在Python中,能够直接处理的数据类型有以下几种: 一.整数 Python可以处理任意大小的整数,当然包括负整数,在Python程序中,整数的表示方法和数学上的写法一模一样,例如:1,100,-80 ...
- Python入门基础之条件判断、循环、dict和set
Python之if语句 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,可以用if语句实现: age = 20 if age >= 18: print 'your age is ...
- Python基础:条件判断 &&循环
1:条件判断 2:循环 2.1:for 2.2 while 小结: continue :跳出本次循环 进行下次循环, break :结束循环体.
- Python下载、环境变量配置、 模块安装方法
下载 Windows版官网下载地址:https://www.python.org/downloads/windows/ 类似下图以 installer结尾的文件就是我们需要下载的,位数根据自己的电脑进 ...
- python计算机硬件基础以及变量常量常量池,解释器编译器比较,python的两种运行方式
1.什么是编程语言 语言是一个事物与另外一个事物沟通的介质 编程语言是程序员与计算机沟通的介质 2.什么是编程 编程就是程序按照某种编程语言的语法规范将自己想要让计算机做的事情表达出来 表达的结果就是 ...
- Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则
时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...
- Python开发基础-Day14正则表达式和re模块
正则表达式 就其本质而言,正则表达式(或 re)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 ...
- Python 3基础教程20-Python中导入模块和包
本文介绍Python中导入模块和包 #目录: # 导入模块和包--- # | # 上级包.上级模块.导入模块和包的init模块----- # | # 同级包.同级模块.上级包的init模块.test模 ...
- python学习基础之变量
变量名只能包含字母.数字和下划线.变量名可以字母或下划线打头,但不能以数字打 头,例如,可将变量命名为message_1,但不能将其命名为1_message. 变量名不能包含空格,但可使用下划线来分隔 ...
随机推荐
- 迭代器对象numpy.nditer在数组上进行迭代——修改数组的值
nditer对象有另一个可选参数op_flags,默认情况下,nditer将视待迭代遍历的数组为只读对象(read-only),为了在遍历数组的同时,实现对数组元素值得修改,必须指定op_flags= ...
- linux下的npm安装
curl --silent --location https://rpm.nodesource.com/setup_10.x | bash - yum install -y nodejs npm in ...
- Java 8 stream 实战
概述 平时工作用python的机会比较多,习惯了python函数式编程的简洁和优雅.切换到java后,对于数据处理的『冗长代码』还是有点不习惯的.有幸的是,Java8版本后,引入了Lambda表达式和 ...
- Plastic Bottle Manufacturer - What Is The Direction Of Plastic Bottles?
Plastic bottle manufacturers explain: protective packaging for cosmetic cleaning products requires b ...
- Navigation源码(一) move_base最全解析
一.概述 目测是全网最全的解析,花了几个小时通读并整理的,供大家参考学习. 概况的话可以看下古月居 https://www.guyuehome.com/270,其实它是翻译官方的,英语ok的可以去ro ...
- vue中 el [$el] 的理解
<template> <div class="a"> <div class="basic" ref="ba"& ...
- luogu P2754 [CTSC1999]家园
本题是分层图最大流问题,相当于按时间拆点,每个当前点向下一点的下一时间层连点,每一层有n+1个点 #include<bits/stdc++.h> using namespace std; ...
- 手机号----IP api
/* *手机号码API */ $fPArr = iconv("gbk","utf-8",file_get_contents($fphone)); echo $f ...
- Centos7 安装编译nginx-1.9.6过程
一.安装环境准备 使用编译安装nginx最好都先安装下这些依赖包 安装nginx需要的依赖库 yum install -y gcc patch libffi-devel python-devel zl ...
- Lesson 8 Trading standards
What makes trading between rich countires difficult? Chickens slautered in the United States, claim ...