python经典实例

python经典实例

ID:21936053

大小:110.05 KB

页数:11页

时间:2018-10-25

python经典实例_第1页
python经典实例_第2页
python经典实例_第3页
python经典实例_第4页
python经典实例_第5页
资源描述:

《python经典实例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、1输出你好#打开新窗口,输入:#! /usr/bin/python# -*- coding: utf8 -*- s1=input("Input your name:")print("你好,%s" % s1)'''知识点:    * input("某字符串")函数:显示"某字符串",并等待用户输入.    * print()函数:如何打印.    * 如何应用中文   * 如何用多行注释'''    2输出字符串和数字但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异

2、,如下:#! /usr/bin/pythona=2b="test"c=a+b运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换#! /usr/bin/python#运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换a=2b="test"c=str(a)+bd="1111"e=a+int(d)#How to print multiply valuesprint ("c is %s,e is %i" % (c,e))'''知识点:    * 用int和str函数将字符串和数字进行转换  

3、  * 打印以#开头,而不是习惯的//    * 打印多个参数的方式    '''3列表#! /usr/bin/python# -*- coding: utf8 -*-#列表类似Javascript的数组,方便易用#定义元组word=['a','b','c','d','e','f','g']#如何通过索引访问元组里的元素a=word[2]print ("a is: "+a)b=word[1:3]print ("b is: ")print (b) # index 1 and 2 elements of word.c=word[:2]

4、print ("c is: ")print (c) # index 0 and 1 elements of word.d=word[0:]print ("d is: ")print (d) # All elements of word.#元组可以合并e=word[:2]+word[2:]print ("e is: ")print (e) # All elements of word.f=word[-1]print ("f is: ")print (f) # The last elements of word.g=word[-4:-

5、2]print ("g is: ")print (g) # index 3 and 4 elements of word.h=word[-2:]print ("h is: ")print (h) # The last two elements.i=word[:-2]print ("i is: ")print (i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+ str(l))print ("Adds new e

6、lement")word.append('h')print (word)#删除元素del word[0]print (word)del word[1:3]print (word)'''知识点:    * 列表长度是动态的,可任意添加删除元素.    * 用索引可以很方便访问元素,甚至返回一个子列表    * 更多方法请参考Python的文档'''4字典#! /usr/bin/pythonx={'a':'aaa','b':'bbb','c':12}print (x['a'])print (x['b'])print (x['c'])f

7、or key in x:    print ("Key is %s and value is %s" % (key,x[key]))    '''知识点:    * 将他当Java的Map来用即可.'''5字符串比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.#! /usr/bin/pythonword="abcdefg"a=word[2]print ("a is: "+a)b=word[1:3]print ("b is: "+b) # index 1 and 2 elements of wor

8、d.c=word[:2]print ("c is: "+c) # index 0 and 1 elements of word.d=word[0:]print ("d is: "+d) # All elements of word.e=word[:

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。