Python教程1,,Python教程11
Python教程1,,Python教程11
Python教程1
1.print
#-*-coding:utf-8-*-print("Hello python")# 这是单行注释print("Hello Hello")print("你好吗") # 这是单行注释""""这是一个多行注释。。。。。。。。注释结束了"""print("hello 全世界")
2.算数运算符
运算符 | 描述 | 实例 |
+ | 加 | 10+20=30 |
- | 减 | 10-20=-10 |
* | 乘 | 10*20=200 |
/ | 除 | 10/20=0.5 |
// | 取整余 | 9//2=4 |
% | 取余数 | 9%2=1 |
** | 幂 | 2**3=9 |
python中*运算符还可以用于字符串,计数结果就是字符串重复指定次数得结果:
print("_ "*10)_ _ _ _ _ _ _ _ _ _
3.变量
# -*-coding:utf-8-*-qq_number = "1234567" # 字符串类型strqq_number1 = 123 # 整数类型intprint(qq_number)print(qq_number1 + 1)price = 8.5 # 浮点类型floatweight = 7.5money = weight * pricemoney = money - 5print (money)gen = True # 布尔类型True或者Falseqq = type(qq_number1) # type函数可以查看变量类型print (qq)print (2 ** 32)print (type(2 ** 16)) # 整型print (2 ** 64)print (type(2 ** 32)) # Python2.x才有长整型# 整型变量得运算i = 10f = 10.5b = True # 如果变量时布尔类型,进行算术运算时,True=1;False=0print(i + f)print(i + b)# 字符串变量之间使用+拼接first_name = "三"last_name = "张"print (last_name + first_name)# 字符串变量和整数使用* 重复拼接相同得字符串,整型和字符串之间不能进去其他计数print ("_ " * 5)# input()函数接收用户输入,input()函数输入都是字符类型str1 = input("请输入银行密码:")print (str1)# 类型转换函数int(x),float(x)int("123")print (type(int("123")))
4.实例
# -*-coding:utf-8-*-# 输入苹果得单价price = float(input("请输入苹果单价:"))# 输入苹果得重量weight = float(input("请输入重量:"))money = price * weightprint (money)
5.格式化输出
# -*-coding:utf-8-*-# 变量得格式化输出""""%s 字符串%d 有符号十进制数,%06d表示输出的整数位数6,不足的地方使用0%f 浮点数,%.02f 表示小数点后显示2位数%% 输出%"""name = "小明"print ("我的名字叫%s,请多多关照!" % name)student_no = 1print ("我的学号是%06d" % student_no)price = 2.5weight = 2.0money = price * weightprint ("苹果的单价%.02f元/kg,购买了%.02fkg,需要支付%.04f元" % (price, weight, money))scale = 0.25print ("数据比例是%0.2f%%" % (scale * 100))
6.关键字包
import keyword # 关键字可以导入一个工具包到内存print (keyword.kwlist) # 查看工具包中的关键字列表
Python教程1
评论关闭