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

python第一天

时间:2017-07-31 22:13:59      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:python

一、Python数据类型

字符串、数字、元组(tup)、list(列表)、dict(字典)

1.数字操作:

 1.1四则运算:+ - * / %(求余)

print 2+2
4
print 1+2*4
9
print 1.0/2.0
0.5
print 2%4
2



2.字符串操作: 

 2.1.字符串:用单引号或者双引号引起来

print ‘hello‘ 
hello


 2.2.字符串拼接:用+号将多个字符串拼接起来

print ‘hello ‘+‘world‘
hello world


 2.3.字符串转义:在Python中字符串使用单引号或双引号括起来,如果字符串内部出现了需要转义的字符(例如:"和‘本身),只需要在字符前添加转义字符:\即可

常用转义字符:

>>> print ‘My name\‘s lilei‘
My name‘s lilei

4.变量:变量值可以是字符串、数字、list、dict、tup

x = ‘hello world‘
print x
hello world
age = 20
print age
20

5.获取用户输入:raw_input()#python内置函数

#代码
x = raw_input(‘please input a string: ‘)
print ‘hello ‘+x
#输入
world
输出 hello world

6.字符串格式化:

常用类型:

%s:字符串

%d:整型

%f:浮点型

%x:十六进制

%%:表示%本身

name = ‘liuyang‘
age = 26
#数字和字符串不能直接相加
#26为数字,不能与字符串相连,所以我们要将类型转换成字符串
print ‘hello ‘+name+‘,and i am ‘+str(age)+‘ years old‘
hello liuyang,and i am 26 years old
#字符串格式化%s 引入变量 后边%结尾意思是左边字符串结束了现在要代入变量)
print ‘hello %s,and i am %s years old‘ %(name,age)
hello liuyang,and i am 26 years old
#用%s换成%d占位符,代表一个数字
>>>print ‘hello %s,and i am %d years old‘ % (name,age)
hello liuyang,and i am 26 years old

7.数据类型

int:整型

str:字符串

float:浮点型

bool:布尔型

x = raw_input(‘print input a number: ‘)
y = raw_input(‘print input a number2: ‘)
print int(x)+int(y)
#输入x 3 输入y 4
#输出 7

8.Python内置函数strip():

.strip()#消除字符串两边空格

#默认是空格,可以是别的字符

.strip(‘\n‘)

#消除换行符

.rstrip()#消除字符串右边空格

.lstrip()#消除字符串左边空格

.upper()大写转小写

.upper().lower() 小写转大写

name = ‘  liuyang  ‘
print name.strip()
liuyang
print name.lstrip()
liuyang  
print name.rstrip()
  liuyang
print name.strip().upper()
LIUYANG
print name.strip().upper().lower()
liuyang

9.Python内置函数strip():

type()

判断数据类型

name = ‘liuyang‘
print type(name)
str

10.流程控制

 10.1.布尔值:True,False

True:表示真

False:表示假

4>3
True
not 0
True
5>6
False

 10.2.逻辑运算:and or not

#A  and B 就是A 和B都是TRUE的时候才是TRUE,其它情况都是false

#A or B 就是A 和B只要有一个是TRUE就是TRUE,条件至成立一个即为TRUE

#not  A如果A是FALSE返回TRUE,如果A 是True返回FALSE

注:‘and’的优先级高于‘or’

 10.3.if else判断:

if 条件判断(真/假):条件为真执行if后面语句

else:#条件为假执行else语句

条件为假的情况:

#空字符串

#空列表

#数字0

#空字典都是

#none

if ‘‘:
   print ‘123‘
else:
   print ‘1231343‘
1231343
#
if not 0:
    print ‘add‘
else:
   print ‘dd‘
add
#
list = []
>>>if list:
   print ‘list不为空为真‘
else:
   print ‘aaa‘     
aaa
#
if dict:
   print ‘123132‘
else:
   print ‘af‘     
af

 10.4.while循环:

#while 条件成立:

#      如果情况是TRUE,代码持续执行;不成立即为False

#while循环直到情况是false才会退出

#while True一直循环,break手动终止

break和continue都是针对for循环和while循环的

break:跳出循环

#continue:跳过本次循环

i = 0

while i<20:

     print i

     i = i + 1

#

>>>name = ‘‘

# 定义name为空,while判断name非空是True,代码执行,

# name空为假,条件判断FALSE退出print name

while not name:

    name=raw_input(‘input your name‘)

print ‘hello ‘+name

 10.5.for 循环序列:专门针对list,dict,以后用到比较多的流程控制

for name in [‘2‘,‘3‘,‘4‘]:
    print name
2 
3 
4

练习1:输入一个数字,不为0时打印所有数字的和,如果数据输入是0直接退出

#/usr/bin/python
#coding=utf-8

sum = 0
while True:
num = int(raw_input(‘input a number‘))
if num == 0:
break
else:
sum = sum + int(num)
print sum
执行脚本:输入23 22 22 输入0 退出脚本,打印输入数字总和67

练习2:本金是10000,年利率是3.25%,求多少钱后,存款能翻翻

#/usr/bin/python
#coding=utf-8
money=10000
lixi=0.0325
y=0
#如果money小于20000条件为true则循环一直执行,flase跳出循环 即money大于20000也就是本金翻翻
while money <= 20000:
money=money*(1+lixi)
y = y + 1
print money,y
#执行脚本
20210.6986788存款翻翻  22年

练习3:打印最大的两个值

#/usr/bin/python
#coding=utf-8
unsort_num = [1,2,3,444,555,3234,35,65535,65535,21]
#print unsort_num
max_num1 = 0
max_num2 = 0
for i in unsort_num:
if i > max_num1:
max_num2 = max_num1
max_num1 = i
elif i > max_num2:
max_num2 = i
print "max num is:%s,second max num is: %s " %(max_num1,max_num2)
#打印结果
max num is:65536 ,second max num is 65535

练习4:用户登录,验证用户及密码,密码输入错误三次退出登录

name = raw_input(‘input your name: ‘).strip()
if name == ‘liuyang‘:
count = 0
while count<3:
passwd = raw_input(‘input your passwd: ‘)
if passwd == ‘123456‘:
print‘User %s login sucessfully !‘%(name)
break
else:
print ‘Wrong passwd,please input again !‘
count +=1
print ‘passwd wrong three times,the count %s is locked !‘%(name)
else:
print "User %s not exists,please confirm !"%(name)

练习5:输入一个数字,与50比较,大于50提示输入数字太大,小于50,提示输入数字太小,三次机会,超过三次,返回输入错误

优化前:

#/usr/bin/python
#coding=utf-8
compare_num = 50
count = 0
while count<=3:
input_num = int(raw_input(‘input a number: ‘))
if input_num == compare_num:
print ‘输入数字正确‘
break
elif input_num > compare_num:
print ‘输入的数字%s大于%s,还有%d次机会‘%(input_num,compare_num,3-count)
else:
print ‘输入的数字%s小于%s,还有%d次机会‘%(input_num,compare_num,3-count)
count +=1
#优化后
#
#/usr/bin/python
#coding=utf-8
import sys
compare_num = 50
count = 0
while count<=3:
input_num = raw_input(‘input a number: ‘)
try:
num = int(num)
except Exception,e:
print ‘输入非法‘
sys.exit()
time = 3-count
if input_num > compare_num:
print ‘输入的数字太大,还有%s次机会‘%(time)
elif input_num < compare_num:
print ‘输入的数字太小,还有%s次机会‘%(time)
else:
print ‘输入数字正确‘
break
count +=1


#引申:

exit():是Python系统函数 import sys #导入系统函数 

break 与exit() exit是直接退出python脚本,break是退出当前循环


异常检查处理

try:

1/0 #执行语句,并返回执行结果

except Exception,e:

print "语法错误"


python第一天

标签:python

原文地址:http://442989.blog.51cto.com/432989/1952427

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