下载此文档

python入门经典实例.doc


文档分类:IT计算机 | 页数:约13页 举报非法文档有奖
1/13
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/13 下载此文档
文档列表 文档介绍
1你好#打开新窗口,输入:#! /usr/bin/python# -*- coding: utf8 -*- s1=input("Input your name:")print("你好,%s" % s1)'''知识点:    * input("某字符串")函数:显示"某字符串",并等待用户输入.    * print()函数:如何打印.    * 如何应用中文   * 如何用多行注释'''    2字符串和数字但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,,如下:#! /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列表#! /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[:2]print ("c is: ")print (c) # index 0 and 1 elements of =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[-1]print ("f is: ")print (f) # The last elements of =word[-4:-2]print ("g is: ")print (g) # index 3 and 4 elements of =word[-2:]print ("h is: ")print (h) # The last two =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 element")('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'])for key in x:    print ("Key is %s and value is %s" % (key,x[key]))    '''知识点:    * 将他当Java的Map来用即可.'''5字符串比起C/C++,.#! /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 =word[:2]print ("c is: "+c) # index 0 and 1 elements of =word[0:]print ("d is: "+d) # All elements of =word[:2]+w

python入门经典实例 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数13
  • 收藏数0 收藏
  • 顶次数0
  • 上传人tswng35
  • 文件大小77 KB
  • 时间2020-02-20