设为首页 收藏本站
查看: 1044|回复: 0

[经验分享] 我的python学习--第一天

[复制链接]

尚未签到

发表于 2018-8-11 11:52:02 | 显示全部楼层 |阅读模式
  前言:
  Python, 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。
  Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议。
  Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。
  Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。
  1、打印字符串
>>> print 'hello world!'  
hello world!
  2、四则运算
>>> print 2+3*40/5  
26
  3、字符串拼接
>>> print 'hello '+'world'  
hello world
  
>>> print 'hello '*3
  
hello hello hello
  4、变量
>>> x = 'hello world'  
>>> print x
  
hello world
  5、获取用户输入
#代码  
x = raw_input('please input your name: ')
  
print 'hello '+x
  

  
#输出
  
please input your name: world
  
hello world
  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\'am the world'  
I'am the world
  
>>> print "I'am the world"
  
I'am the world
  
>>> print 'I'am the world'
  
  File "<stdin>", line 1
  
    print 'I'am the world'
  
              ^
  
SyntaxError: invalid syntax
  8、字符串格式化输出
  %s:字符串
  %d:整型
  %f:浮点型
  %x:十六进制
  %%:表示%本身
>>> x = 'world'  
>>> age = 20
  
>>> print 'hello %s your name is %d'%(x,age)
  
hello world your name is 20
  9、布尔值
  True 表示‘真’
  False 表示‘假’
>>> not 0  
True
  
>>> 2>3
  
False
  10、逻辑运算
  and:与
  or:或
  not:非
  注:‘and’的优先级高于‘or’
>>> True and False  
False
  
>>> True and True
  
True
  
>>> True or False
  
True
  
>>> not True
  
False
  
>>> True and True or False
  
True
  11、条件运算符
  1. if语句
>>> if 2>3:  
...     print 'condition is True'
  
... else:
  
...     print 'condition is False'
  
...
  
condition is False
  1.1 多条件判断
>>> a = 3  
>>> b = 3
  
>>> if a>b:
  
...     print 'a>b'
  
... elif a<b:
  
...     print 'a<b'
  
... else:
  
...     print 'a=b'
  
...
  
a=b
  1.2 if else嵌套
>>> if name=='world':  
...     if age>15:
  
...         print 'you are %d years old'%age
  
... else:
  
...     print 'no message'
  
...
  
you are 20 years old
  2、while循环
>>> i = 0  
>>> while i<5:
  
...     print i
  
...     i += 1        # i += 1 等于 i = i + 1
  
...
  
0
  
1
  
2
  
3
  
4
  3、for循环
  专门针对list,dict等结构
>>> l = ['a','b','c']  
>>> for i in l:
  
...     print i
  
...
  
a
  
b
  
c
  13、break和continue
  break和continue都是针对for循环和while循环的
  break:跳出循环
  continue:跳过本次循环
  14、判断值得真假,可以使用not
>>> not 1  
False
  
>>> not 0
  
True
  15、list和dict
  list:列表,一种有序集合,可以随时添加删除其中的元素
  dict:字典,使用k/v格式存储,可以使用‘in’判断key是否存在
  练习:
  1、需求:让用户一直输入数字(只输入数字),如果输入为0,终止程序。打印所有输入数字的平均值
count = 0  
times = 0
  
while True:
  
    num = input('please input number:')
  
    if num==0:
  
        break
  
    times += 1
  
    count += num
  
    avg = (count+0.0)/times
  
print avg
  2、银行存款10000,年利率3.25%,多少年之后,存款能到翻一番
money = 10000  
year = 0
  

  
while money<20000:
  
    money *= 1.0325
  
    year += 1
  
print '%s years'%year
  3、遍历一个序列['C','js','python','css','js','html','node'],统计这个序列中’js‘出现的次数
l = ['C','js','python','css','js','html','node']  
times = 0
  
for i in l:
  
    if i=='js':
  
        times += 1
  
print times
  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 = 0
  
for i in l:
  
    if i>max:
  
        max = i
  
print 'The max number is %s'%max
  5、用户输入数字,判断是不是闰年(闰年判断规则:1、如果是100的倍数,要被400整除 2、被4整除),如果输入的不是闰年,提示信息,并继续输入
#!/usr/bin/python  
# -*- coding=utf-8 -*-
  

  
while True:
  
    year = input('please input year: ')
  
    if year%400==0 or (year%100!=0 and year%4==0):
  
        print '是闰年'
  
        break
  
    else:
  
        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 = {}
  
for i in l:
  
    if i in d:
  
        d += 1
  
    else:
  
        d = 1
  
print d

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-550112-1-1.html 上篇帖子: Python--day8--Socket编程/异常处理 下篇帖子: python比较两个excel表格的差异
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表