输入输出
标识符
1.以数字字母‘_’(下划线)组成
2.不能以数字作为首字符
3.大小写敏感(Str与str为不同的标识符)
注:一般不一下划线开头,以下划线开头的标识符在解释器中是内解标识符,为私有的;常量名完全大写。
注释
单行注释:
print('注释')# 此内容为注释
多行注释:
'''此内容
为注释'''
"""此内容
是注释"""
输入输出语句
输出
a = 12
print(a)
print('输出%d', %a) # %d,%f,%s用法类似C语言,注意逗号后边用法 %变量名
print('study') # %r 为输出任意类型变量
print('study', a, 'year')
s = 'english'
print('study' + s) # python中允许字符串加法
# 注意只允许同类型变量相加
转义字符及其用法
字符串前缀u,b,r,f用法
print函数中的默认变量sep和end
sep表示分隔符,end代表换行。print函数中默认‘,‘代表空格,末尾默认换行
str = 'pig'
print('It is a', str)
print('It is a', str, sep='*')
********************************
输出结果为:
It is a pig
It is a*pig
print('My', end=' ')
print('name')
*********************************
输出结果:
My name
输入
strt = inpurt('输入提示语句') # 默认输入类型为str类型
a = int(input('输入提示语')) # 输入整型
b = float(inout()) # 输入浮点型
''' 或者
a = input()
a=int(a) '''
定义函数
形式:def 函数名():
可有参数也可以没有参数;一般没有返回值,也可以设置返回值。
def print_content(a): # 可没有参数
print('test')
return a # 返回值,可没有
print_content(11) # 调用方法,输出结果 test
num = print_content(11)
print(num) # 输出结果 test回车11
注:def print_content():
下一行语句前要按一次Tab键,表示此语句是函数中的内容,函数结束后要与下面的语句空两行,即使下面的语句是注释。
若定义的函数没有具体功能,函数不能为空,要写成以下格式
def other(): #空函数
pass
if 1>7: # 空if函数
pass
如果函数的函数体为pass或运行出错,其返回值是None
,它是一个特定值。
num = 10
def test():
# global num
num = 100
test()
print(num)
以上程序结果为10,加上注释内容后结果变为100
在python中局部不能调用全局变量,想调用要加上global 变量名
才行
global num
作用是调用调用全局变量中的num变量。
数据类型
- 整型(int):只能为整数(python2.2以后统一了整形和长整型)
- 字符串(string):表示形式
str1='学习'
str2="study"
str3='''st'u'dy'''
str4='''I "love"
study'''
str5="""st'u'dy"""
str6="""I
"love" study"""
- 元组
one_tuple = (1,5,3,6,7)
two_tuple = 1,5,3,6,7 # 两种定义方式
- 浮点型(float):即数学中的实数。例如3.22,98e3(9800.0)。
- 复数型:2(实部)+4j(虚部)例:2+6j
复数.real 分离实部
复数.imag 分离虚部
复数.conjugate() 求共轭复数
- 列表
def printlist():
print(one_list[2]) # 输出列表中的第几个元素
print(one_list[-1]) # 输出列表中的最后一个元素
print(one_list[1:3]) # 输出第一个到第三个元素
print(one_list[:2]) # 输出从零到第二个元素
def num1(): # 计算列表元素个数
return len(one_list)
def add(): # 在列表中插入一个元素
one_list.append(-33) # 在列表末尾插入一个元素
print(one_list)
one_list.insert(2, 524) # 在列表第二位后插入一个元素
print(one_list)
def del_list():
del one_list[1] # 删除列表第一个元素
print(one_list)
one_list.remove(6) # 删除列表中的某一个元素
print(one_list)
a = one_list.pp() # 将列表中的最后一个元素赋值给a并删除最后一个元素
print(a)
print(one_list)
def num2(): # 找出列表中某个元素的位置
print(one_list.index(6))
def num3(): # 计算列表中某一元素个数
print(one_list.count(6))
def sort_list(): # 列表元素排序
print(one_list.sort()) # 正序
print(one_list.sort(reverse=True)) # 倒序
one_list = [] # 列表元素位置从零开始排
for i in range(7): # 定义一个列表
num = 2*i
one_list.append(num)
# 制造的列表:one_list = [0, 2, 4, 6, 8, 10, 12]
print(one_list)
- 字典:映射类型(类似于哈斯表的键值对)
first_dic = {'one': 1, 2: 'two', 'second': 'two'}
second_dic = {1: 'one', 0: 'two', 4: 'three', 3: 'four'}
third_dic = {1: {'one': 1, 'two': 2}, 2: 'open'}
def Sort(): # 给keys排序,不改变字典keys原顺序
print(sorted(second_dic.keys()))
def Add(): # 插入一对键值对,插入位置随机
first_dic['three'] = 3
print(first_dic)
def Delete(): # 删除字典中的某一对键值对
del first_dic['one']
print(first_dic)
def Change(): # 改变key对应的值
first_dic['one'] = 8
print(first_dic)
def Show():
print(first_dic.items()) # 查看键值对
for key, value in first_dic.items():
print('key:', key, '\t', 'value:', value)
for key in first_dic.keys():
print(key)
for value in first_dic.values():
print(value)
first_dic = {'one': 1, 2: 'two', 'second': 'two'}
second_dic = {1: 'one', 0: 'two', 4: 'three', 3: 'four'}
third_dic = {1: {'one': 1, 'two': 2}, 2: 'open'}
print(first_dic)
print(second_dic)
print(third_dic[1]['one'])
- 布尔型(整型分支):只分为True(数值为0)和False(数值为0)
多文件调用
在一个python文件中调用另一个python文件中的函数。 例:
# test2.py
def show():
print("I am fine")
# test1.py
import test2
test2.show()
**********************
输出结果:
I am fine
注:若test2.py中除了想调用的函数外还有其他功能语句,test1.py将会连其他语句一起调用
# test2.py
def show():
print('I am fine')
print('others')
test1.py运行结果:
others
I am fine
解决方法:
# test2
def show():
print("I am fine")
if __name__ == '__main__' # 判断当前模块__name__
print("others")
在python中*.py文件也可以称为模块。在多文件编程中会分主模块和次模块,在主模块中__name__ == ‘main’,在次模块中__name__ == ‘”文件名“’
需要运行的文件是主模块,被调用的文件是次模块。
import用法
- import test2 (引入test2中所有语句)test1中调用方法:test2.show()
- from test2 import show (只引入test2中的show()函数),test1中调用方法:show()
- from test2 import *(同 import test2),调用方法:show()
- import test2 as t (将test2设置别名为t)test1中调用方法:t.show()