栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python总结与对应练习(一)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python总结与对应练习(一)

一、变量及简单的数据类型 (一)变量

1、python是一门弱数据类型语言。

print()函数——打印输出                type()函数——查看类型

2、变量命名规范

(1)变量名只能包括字母、数字和下划线,且不能以数字开头。

       小驼峰式    userNname;下划线    user_name(推荐)

(2)不能将python关键字或者保留字用作变量名。

(3)变量命名的时候一定要有意义

(二)常用数据类型

1、数值型:

整型int
浮点数float
复数(虚数)一般不用

2、布尔型(bool):True、False

3、字符型(str):用双引号或者单引号括起来的任意长度的文本

4、复合数据类型(引用数据类型):字典、列表、元组、集合

二、常见运算符 (一)算术运算符
运算符名称
+
-
*
/
//整除
%取余
**幂次方
(二)比较(关系)运算符
运算符名称
>大于
<小于
>=大于等于
<=

小于等于

==等于
!=不等于
(三)逻辑运算符
运算符名称
and与,且(一假则假)
or或(一真则真)
not非(一般与in关键字连用)
(四)赋值运算符(常用)
运算符例子
=a = 10
+=a += 10
-=a -= 10
*=a *= 10
/=a /= 10
%=a %= 10
**=a **=10
三、对应练习

一、基本语法(1~18)

Demo01

 

celsius = float(input("输入摄氏度:"))
fahrenheit = (9 / 5) * celsius + 32
print("%s Celsius is %.1f" %(celsius,fahrenheit))

Demo02

radius = float(input("输入圆柱半径:"))
length = float(input("输入圆柱的高:"))
pi = 3.14
area = radius * radius * pi
volume = area * length
print("The area is %.4f" %area)
print("The volume is %.1f" %volume)

Demo03

num = int(input("Enter a number between 0 and 1000:"))
a = num // 100
b = num % 10
c = num // 10 % 10
sum = a + b + c
print("The sum of the digits is %s"%sum)

Demo04

a = float(input("Enter a number of minutes:"))
years = int(a/365/24/60)
days = int(a%(365*60*24)/24*60)
print("%s minutes is approximately %s years and %s days"%(a,years,days))

Demo05

M = float(input("Enter the amoust of water in kilograms:"))
initialTemperture = float(input("Enter the initial temperture:"))
fianlTemperture = float(input("Enter the fianl temperture:"))
Q = M *(fianlTemperture - initialTemperture) * 4184
print("The energy needer is " + str(Q))

Demo06

t = float(input("Enter the temperture in Fahrenheit between -58 and 41:"))
v = float(input("Enter the wind speed in miles per hour:"))
twc = 35.74 + 0.6215*t - 35.75 * (v ** 0.16) + 0.4275 * t * (v **0.16)
print("The wind chill index is %.5f" %(twc))

Demo07

v,a = eval(input("Enter speed and acceleration:"))
length = v**2/2*a
print("The minimum runway length for this airplane is %0.3f meters"%(length))

Demo08

num = int(input("Enter an integer:"))
a = num % 10 #个位
b = num // 10 % 10 #十位
c = num // 100 % 10 #百位
d = num // 1000 % 10 #千位
print("%dn%dn%dn%dn" %(a,b,c,d))

Demo09

x1,y1,x2,y2,x3,y3 = eval(input("Enter three point for a triangle:"))
side1 = ((x1-x2)**2+(y1-y2)**2)**0.5
side2 = ((x2-x3)**2+(y2-y3)**2)**0.5
side3 = ((x1-x3)**2+(y1-y3)**2)**0.5
s = (side1+side2+side3)/2
area = (s * (s-side1) * (s-side2) * (s-side3))**0.5
print("The area of the triangle is %.1f" %(area))

Demo10

s = float(input("Enter the side:"))
area = (3 * (3 ** 0.5) / 2) * (s ** 2)
print("The area of the hexagon is %.4f" % (area))

Demo12

a = int(input("Enter the monthly saving amoust:"))
b = 1 + 0.00417
first = a * b
second = (a + first) * b
third = (a + second) * b
fourth = (a + third) * b
fifth = (a + fourth) * b
sixth = (a + fifth) * b
print("After the sixth month,the account value is %.2f" %(sixth))

Demo13

a = float(input("Enter investment amount:"))
b = float(input("Enter annual interest rate:"))
years = float(input("Enter number of years:"))
month = 12 * years
accumulated = a * (1 + b * 0.01 / 12) ** month
print("Accumulated value is %.2f" % (accumulated))

Demo14

import math
a,b,c= eval(input("Enter three point for a triangle:"))
A = round(math.degrees(math.acos((a * a - b * b - c * c) / (-2 * b * c))))
B = round(math.degrees(math.acos((b * b - a * a - c * c) / (-2 * a * c))))
C = round(math.degrees(math.acos((c * c - b * b - a * a) / (-2 * a * b))))
print("A=%d,B=%d,C=%d"%(A,B,C))

Demo15

import math
n = float(input("Enter the number of sides:"))
s = float(input("Enter the side:"))
area = n * s ** 2 / (4 * math.tan(math.pi / n))
print("The area of the polygon is %s" %(area))

Demo16

num = int(input("Enter an integer:"))
a = num % 10 #个位
b = num // 10 % 10 #十位
c = num // 100 % 10 #百位
d = num // 1000 % 10 #千位
print("%d%d%d%d" %(a,b,c,d))

Demo17

num = float(input("输入一个十进制带小数点的数字:"))
a = num * 100      #将钱数转换成分数
b = a // 100       #美元个数
c = a % 100        #剩余分数
d = c // 25        #两角五分硬币个数
e = c % 25         #换算后剩余分数
f = e // 10        #一角硬币个数
g = e % 10         #换算后剩余分数
h = g // 5         #五分硬币个数
i = g % 5          #换算后剩余分数
print("美元个数为:%d, 二角五分硬币个数为:%d,一角硬币个数为:%d,五分硬币个数为:%d,一美分个数为:%d" % (b,d,f,h,i))

Demo18

name = input("Enter employee's name:")
time = float(input("Enter number of hours worked in a week:"))
pay = float(input("Enter hourly pay rate:"))
federal = float(input("Enter federal tax withholding rate:"))
state = float(input("Enter state tax withholding rate:"))
reward = time * pay
a = reward * federal
b = reward * state
c = federal * 100
d = state * 100
sum = a + b
nat = reward - sum
print("Employee's name:%s" %name)
print("Hours Worked:%.1f" %time)
print("Pay Rate:$%.2f" %pay)
print("Gross Pay:$%.1f" %reward)
print("Deductions:")
print("   Federal Withholding (%.1f%%):$%.2f" %(c,a))
print("   State Withholding (%.1f%%):$%.2f" %(d,b))
print("   Total Deduction:$%.2f" % sum)
print("Net Pay:$%.2f" % nat)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/357183.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号