1.python基础知识
-
语法:
1.没有分号
2.严格按照缩进的语言
#coding:utf-8
print(‘hhh’) -
注释:
单行注释
#单行
多行注释
“”"
多行注释
“”"
2.python3.6与python2的不同
1)字符类型不同
python3.6的字符类型有:int、float、str、boolean
str:
In [11]: name = '张三'
In [12]: name
Out[12]: '张三'
int:
In [13]: age = 22
In [14]: age
Out[14]: 22
boolean:
In [15]: gender =True
In [16]: gender
Out[16]: True
float:
In [17]: grade = 99.28
In [18]: grade
Out[18]: 99.28
python2的字符类型有:长整型、int、float、str、boolean
2)接收默认类型不同
python2的默认接收类型为int
raw_input接收类型默认为str
ipthon3.6的默认输入类型为str
python3.6没有raw_input命令
3.python3.6字符操作
-
输出百分数:
In [6]: scale = 0.1
In [9]: print(‘数据比例是 %.2f%%’ %(scale*100))
数据比例是 10.00% -
删除变量
-
布尔值
-
字符居中
-
字符转换
练习1:(编写python文件)[root@localhost mnt]# cat grade.py
name = input(“学生姓名:”)
ch= float(input(“语文:”))
math= float(input(“数学:”))
en = float(input(“英语:”))Sum = ch + math + en
avg = Sum/3print("%s的平均成绩为%.2f" %(name,avg))
执行结果:
练习2:(在pycharm中编程)""" # _*_coding:utf-8_*_ Name:01_test.py Date:19-1-12 Author:hr """ """ 石头剪刀布 电脑随机出拳 """ a = int(input("请出拳")) import random b = random.randint(1, 3) print(b) if a == b: print("平局") elif ((a == 1 and b == 3) or (a == 2 and b == 1) or (a == 3 and b == 2)): print("计算机胜利") else: print("计算机失败")
练习三:
"""
判断闰年
能被400整出的是闰年,能被4整chu但不能被100的是闰年
"""
year = int(input('请输入年份'))
if ((year % 400==0) or (year % 4 ==0 and year % 100 != 0)):
print('你输入的是闰年')
else:
print('不是闰年')