最近在学习python这个广受欢迎的脚本语言,别说,语法感觉比Java,C/C++要松得多。
基本结构训练例子(包含字符,数字,dict,list,tuple,set[未举例],条件语句,循环语句等等):
# Test.py
# -*- coding: utf-8 -*-
print('hello world!')
print('''hello1
...hello2
...''' + chr(31212) + "\n中文显示")
print("您好%s,欢迎您的到来,您的登录号为:%d" % ("蒲xx", 1))
# list有序集合[]
jihe = ["hello1", "hello2", 'nihao', 1211]
print(jihe)
jihe.pop(1)
print(jihe)
jihe.insert(1, "Java高级")
print(jihe[1])
# tuple不能改变的有序集合()
chushi = ("hello4", "hello5", "hello python", 22222)
# for in循环
for x in [1, 2, 3, 4, 5, 6, 7]:
print("data:%s" % x)
print(chushi)
# 字典dict {key:value}
data = {1: "java", 2: "python", 3: "JS"}
print(data[2])
data.pop(3)
print(data)
# set集合 无序不重复集合 可进行数学集合运算 如 与 或 非
# while循环
'''
str
while str != '0':
str = input()
print(str)
print("Over!")
'''
def judge_age(age):
if not isinstance(age, (float, int)):
raise TypeError('bad operand type')
if age >= 18:
print("%s的您已经成年了" % age)
else:
print("%s岁,小屁孩,还不上学去!" % age)
return 0
# age = int( input( "您的年龄:") )
judge_age(9)
—2019.07.09—
函数训练例子(包含函数调用,函数定义,函数必选参数、默认参数、可变参数、关键字参数、命名关键字参数,函数文档查看):
#Function.py
# -*- coding: utf-8 -*-
import math
def move(x, y, step, angle=0):
'''
坐标移动 (位置参数 默认参数)
:param x:
:param y:
:param step:
:param angle:
:return x,y:
'''
x = x + step * math.cos(angle)
y = y + step * math.sin(angle)
return x, y
x, y = move(100, 100, 20, math.pi / 3)
print(x, y)
def sum(*number):
"""
集合求和 (可变参数:传一个tuple 或者list进来)
:param number:
:return:
"""
sum = 0;
for x in number:
sum += x
return sum
print(sum(1, 2, 5, 688, 90)) # 将这写数字转为list 或 tuple 后传值
# 扩展函数作用
def info(name, sex, **info):
"""
显示dict信息(
关键字参数
传入0 - 任意个参数名[key]
及参数值[value])
:param name:
:param sex:
:param info:
:return 0:
"""
print("name:", name, " sex:", sex, " info:", info)
return 0
info("macy", "男", addr="cq", phone="18181", age=23)
def student( ID, name,sex, *,addr="china",phone):
"""
命名关键字参数
1.在参数列表中加一个特殊分隔符“*”,在此分隔符后,传参数需加上参数名
2.如参数列表中有可变参数,则不需加特殊分隔符“*”
:param ID:
:param name:
:param sex:
:param addr:
:param phone:
:return 0:
"""
print("ID:", ID, "name:", name, "sex:", sex, "addr:", addr, "phone:", phone)
return 0
student(10001,"Pacal", "男",addr="BeiJing",phone="12306") #必须加上参数名
student(10002,"Macy","女",addr="The U.S.A",phone="12121")
#函数文档打印
print(student.__doc__)