我的python学习--第一天,,前言: Python
我的python学习--第一天,,前言: Python
前言:
Python, 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。
Python是纯粹的自由软件,源代码和解释器CPython遵循GPL(GNUGeneral Public License)协议。
Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。
Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。
1、打印字符串
>>>print‘helloworld!‘helloworld!
2、四则运算
>>>print2+3*40/526
3、字符串拼接
>>>print‘hello‘+‘world‘helloworld>>>print‘hello‘*3hellohellohello
4、变量
>>>x=‘helloworld‘>>>printxhelloworld
5、获取用户输入
#代码x=raw_input(‘pleaseinputyourname:‘)print‘hello‘+x#输出pleaseinputyourname:worldhelloworld
raw_input()函数,输入的值会做字符串处理
input()函数,输入的值为原始type
6、数据类型
int:整型
str:字符串
float:浮点型
bool:布尔型
可以通过type()查看类型
>>>a=5>>>x=‘a‘>>>type(x)<type‘str‘>>>>type(a)<type‘int‘>
7、转义字符‘\’
>>>print‘I\‘amtheworld‘I‘amtheworld>>>print"I‘amtheworld"I‘amtheworld>>>print‘I‘amtheworld‘File"<stdin>",line1print‘I‘amtheworld‘^SyntaxError:invalidsyntax
8、字符串格式化输出
%s:字符串
%d:整型
%f:浮点型
%x:十六进制
%%:表示%本身
>>>x=‘world‘>>>age=20>>>print‘hello%syournameis%d‘%(x,age)helloworldyournameis20
9、布尔值
True 表示‘真’
False 表示‘假’
>>>not0True>>>2>3False
10、逻辑运算
and:与
or:或
not:非
注:‘and’的优先级高于‘or’
>>>TrueandFalseFalse>>>TrueandTrueTrue>>>TrueorFalseTrue>>>notTrueFalse>>>TrueandTrueorFalseTrue
11、条件运算符
1. if语句
>>>if2>3:...print‘conditionisTrue‘...else:...print‘conditionisFalse‘...conditionisFalse
1.1 多条件判断
>>>a=3>>>b=3>>>ifa>b:...print‘a>b‘...elifa<b:...print‘a<b‘...else:...print‘a=b‘...a=b
1.2 if else嵌套
>>>ifname==‘world‘:...ifage>15:...print‘youare%dyearsold‘%age...else:...print‘nomessage‘...youare20yearsold
2、while循环
>>>i=0>>>whilei<5:...printi...i+=1#i+=1等于i=i+1...01234
3、for循环
专门针对list,dict等结构
>>>l=[‘a‘,‘b‘,‘c‘]>>>foriinl:...printi...abc
13、break和continue
break和continue都是针对for循环和while循环的
break:跳出循环
continue:跳过本次循环
14、判断值得真假,可以使用not
>>>not1False>>>not0True
15、list和dict
list:列表,一种有序集合,可以随时添加删除其中的元素
dict:字典,使用k/v格式存储,可以使用‘in’判断key是否存在
练习:
1、需求:让用户一直输入数字(只输入数字),如果输入为0,终止程序。打印所有输入数字的平均值
count=0times=0whileTrue:num=input(‘pleaseinputnumber:‘)ifnum==0:breaktimes+=1count+=numavg=(count+0.0)/timesprintavg
2、银行存款10000,年利率3.25%,多少年之后,存款能到翻一番
money=10000year=0whilemoney<20000:money*=1.0325year+=1print‘%syears‘%year
3、遍历一个序列[‘C‘,‘js‘,‘python‘,‘css‘,‘js‘,‘html‘,‘node‘],统计这个序列中’js‘出现的次数
l=[‘C‘,‘js‘,‘python‘,‘css‘,‘js‘,‘html‘,‘node‘]times=0foriinl:ifi==‘js‘:times+=1printtimes
4、求[1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]这个序列的最大值
l=[1,2,3,2,12,3,1,3,21,2,2,3,4111,22,3333,444,111,4,5,777,65555,45,33,45]max=0foriinl:ifi>max:max=iprint‘Themaxnumberis%s‘%max
5、用户输入数字,判断是不是闰年(闰年判断规则:1、如果是100的倍数,要被400整除 2、被4整除),如果输入的不是闰年,提示信息,并继续输入
#!/usr/bin/python#-*-coding=utf-8-*-whileTrue:year=input(‘pleaseinputyear:‘)ifyear%400==0or(year%100!=0andyear%4==0):print‘是闰年‘breakelse:print‘不是闰年‘
6[‘C‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘],求这个list中,每个元素出现的次数
l=[‘C‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘,‘js‘,‘python‘,‘js‘,‘css‘,‘js‘,‘html‘,‘node‘]d={}foriinl:ifiind:d[i]+=1else:d[i]=1printd
我的python学习--第一天
评论关闭