-------------------2020-03-28----------------------------
输出:
print("q","w"); q w输入:
#name = input('please enter your name: ') name = input() print(name); qwe qwe整数,浮点数,字符串,布尔值,空值(None)
在 Python 里,标识符由字母、数字、下划线组成。 在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。 Python 中的标识符是区分大小写的。 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入。 以双下划线开头的 __foo 代表类的私有成员,以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数。 Python 可以同一行显示多条语句,方法是用分号 ; 分开
Python语句中一般以新行作为语句的结束符。 但是我们可以使用斜杠( \)将一行的语句分为多行显示,如下所示:
total = item_one + \ item_two + \ item_three-------------------2019-12-13----------------------------
交互式编程不需要创建脚本文件,是通过 Python 解释器的交互模式进来编写代码。
linux上你只需要在命令行中输入 Python 命令即可启动交互式编程,提示窗口如下:
$ python Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print ("Hello, Python!") Hello, Python!test.py
#!/usr/bin/python print ("Hello, Python!")执行:
chmod +x test.py # 脚本文件添加可执行权限 python test.py Hello, Python!在 Python 里,标识符由字母、数字、下划线组成。 在 Python 中,所有标识符可以包括英文、数字以及下划线(_),但不能以数字开头。 Python 中的标识符是区分大小写的。 以下划线开头的标识符是有特殊意义的。以单下划线开头 _foo 的代表不能直接访问的类属性,需通过类提供的接口进行访问,不能用 from xxx import * 而导入。 以双下划线开头的 __foo 代表类的私有成员,以双下划线开头和结尾的 __foo__ 代表 Python 里特殊方法专用的标识,如 __init__() 代表类的构造函数。
Python 可以同一行显示多条语句,方法是用分号 ; 分开
Python语句中一般以新行作为语句的结束符。
但是我们可以使用斜杠(\)将一行的语句分为多行显示,如下所示:
total = item_one + \ item_two + \ item_threePython有五个标准的数据类型: -Numbers(数字) -String(字符串) -List(列表) -Tuple(元组) -Dictionary(字典)
int(有符号整型) long(长整型[也可以代表八进制和十六进制]) float(浮点型) complex(复数)
List(列表) 是 Python 中使用最频繁的数据类型,可以理解为C语言中的数组。 列表可以完成大多数集合类的数据结构实现。它支持字符,数字,字符串甚至可以包含列表(即嵌套)。 列表用 [ ] 标识,是 python 最通用的复合数据类型。
#!/usr/bin/python # -*- coding: UTF-8 -*- list = [ 'runoob', 786 , 2.23, 'john', 70.2 ] print list[1:3] # 输出第二个至第三个元素 print list[2:] # 输出从第三个开始至列表末尾的所有元素 [786, 2.23] [2.23, 'john', 70.2]元组是另一个数据类型,类似于 List(列表),可以理解为常量数组。 元组用 () 标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。
#!/usr/bin/python # -*- coding: UTF-8 -*- tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 ) print tuple[1:3] # 输出第二个至第四个(不包含)的元素 print tuple[2:] # 输出从第三个开始至列表末尾的所有元素 (786, 2.23) (2.23, 'john', 70.2)字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合,字典是无序的对象集合。 两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移存取。 字典用"{ }"标识。字典由索引(key)和它对应的值value组成。
#!/usr/bin/python # -*- coding: UTF-8 -*- dict = {} dict['one'] = "This is one" dict[2] = "This is two" tinydict = {'name': 'john','code':6734, 'dept': 'sales'} print dict['one'] # 输出键为'one' 的值 print dict[2] # 输出键为 2 的值 print tinydict # 输出完整的字典 print tinydict.keys() # 输出所有键 print tinydict.values() # 输出所有值 This is one This is two {'dept': 'sales', 'code': 6734, 'name': 'john'} ['dept', 'code', 'name'] ['sales', 6734, 'john']Python 编程中 if 语句用于控制程序的执行,基本形式为:
if 判断条件: 执行语句…… else: 执行语句…… if 判断条件1: 执行语句1…… elif 判断条件2: 执行语句2…… elif 判断条件3: 执行语句3…… else: 执行语句4……例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- flag = False name = 'luren' if name == 'python': # 判断变量是否为 python flag = True # 条件成立时设置标志为真 print 'welcome boss' # 并输出欢迎信息 else: print name # 条件不成立时输出变量名称 luren例子:
#!/usr/bin/python count = 0 while (count < 9): print 'The count is:', count count = count + 1 print "Good bye!" The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- for letter in 'Python': # 第一个实例 print '当前字母 :', letter fruits = ['banana', 'apple', 'mango'] for fruit in fruits: # 第二个实例 print '当前水果 :', fruit print "Good bye!" 当前字母 : P 当前字母 : y 当前字母 : t 当前字母 : h 当前字母 : o 当前字母 : n 当前水果 : banana 当前水果 : apple 当前水果 : mango Good bye!例子1:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 定义函数 def printme( str ): "打印任何传入的字符串" print str; return; # 调用函数 printme("我要调用用户自定义函数!"); printme("再次调用同一函数"); 我要调用用户自定义函数! 再次调用同一函数例子2:
#!/usr/bin/python # -*- coding: UTF-8 -*- #可写函数说明 def printinfo( name, age ): "打印任何传入的字符串" print "Name: ", name; print "Age ", age; return; #调用printinfo函数 printinfo( age=50, name="miki" ); Name: miki Age 50Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句。 模块让你能够有逻辑地组织你的 Python 代码段。 把相关的代码分配到一个模块里能让你的代码更好用,更易懂。 模块能定义函数,类和变量,模块里也能包含可执行的代码。
# 导入整个模块 import support # 导入模块中的某个函数 from fib import fibonacci # 导入整个模块 from modname import *raw_input([prompt]) 函数从标准输入读取一个行,并返回一个字符串(去掉结尾的换行符):
str = raw_input("请输入:")input([prompt]) 函数和 raw_input([prompt]) 函数基本类似,但是 input 可以接收一个Python表达式作为输入,并将运算结果返回。
一个文件被打开后,你有一个file对象,你可以得到有关该文件的各种信息。 以下是和file对象相关的所有属性的列表:
属性描述file.closed返回true如果文件已被关闭,否则返回false。file.mode返回被打开文件的访问模式。file.name返回文件的名称。file.softspace如果用print输出后,必须跟一个空格符,则返回false。否则返回true。例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开一个文件 fo = open("foo.txt", "w") print "文件名: ", fo.name print "是否已关闭 : ", fo.closed print "访问模式 : ", fo.mode print "末尾是否强制加空格 : ", fo.softspace 文件名: foo.txt 是否已关闭 : False 访问模式 : w 末尾是否强制加空格 : 0例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开一个文件 fo = open("foo.txt", "w") fo.write( "www.runoob.com!\nVery good site!\n") # 关闭打开的文件 fo.close() $ cat foo.txt www.runoob.com! Very good site!例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开一个文件 fo = open("foo.txt", "r+") str = fo.read(10) print "读取的字符串是 : ", str # 关闭打开的文件 fo.close() 读取的字符串是 : www.runoobtell()方法告诉你文件内的当前位置, 换句话说,下一次的读写会发生在文件开头这么多字节之后。 seek(offset [,from])方法改变当前文件的位置。Offset变量表示要移动的字节数。From变量指定开始移动字节的参考位置。
#!/usr/bin/python # -*- coding: UTF-8 -*- # 打开一个文件 fo = open("foo.txt", "r+") str = fo.read(10) print "读取的字符串是 : ", str # 查找当前位置 position = fo.tell() print "当前文件位置 : ", position # 把指针再次重新定位到文件开头 position = fo.seek(0, 0) str = fo.read(10) print "重新读取字符串 : ", str # 关闭打开的文件 fo.close() 读取的字符串是 : www.runoob 当前文件位置 : 10 重新读取字符串 : www.runoob例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- try: fh = open("testfile", "w") fh.write("这是一个测试文件,用于测试异常!!") except IOError: print "Error: 没有找到文件或读取文件失败" else: print "内容写入文件成功" fh.close()例子:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Employee: '所有员工的基类' empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print "Total Employee %d" % Employee.empCount def displayEmployee(self): print "Name : ", self.name, ", Salary: ", self.salaryself代表类的实例,而非类
如果你的父类方法的功能不能满足你的需求,你可以在子类重写你父类的方法:
#!/usr/bin/python # -*- coding: UTF-8 -*- class Parent: # 定义父类 def myMethod(self): print '调用父类方法' class Child(Parent): # 定义子类 def myMethod(self): print '调用子类方法' c = Child() # 子类实例 c.myMethod() # 子类调用重写方法 调用子类方法参考链接: https://www.runoob.com/python/python-variable-types.html