3.数据类型和变量---用Python做数学运算
变量----保存内容的地方
变量名---以字母为开头,其他的字符必须是字母、数字、下划线,区分大小写,中文也可以,但不推荐。
举例
my_name = "Bryson"
my_age = 43
your_name = input("What is your name? ")
your_age = input("How old are you? ")
print("My name is", my_name, ", and I am", my_age, "years old.")
print("Your name is", your_name, ", and you are", your_age, ".")
print("Thank you for buying my book,", your_name, "!")
Python中的数字和数学运算
数字类型主要是整数和浮点数(带有小数点的数字)
简单的数据类型:布尔值(Boolean),存储了True或False
操作符:+ 、-、*、 /、 **求幂、 ()分组
表达式:数学题
练习:在命令行中输入数学题,计算结果
语法错误:Syntax Error,语法就是语句要遵守的规则
赋值语句:变量=表达式
真除法:Python的/是真除法,例如5/2=2.5
举例:养成写注释的习惯,程序的步骤够成了算法
# AtlantaPizza.py – a simple pizza cost calculator
# Ask the person how many pizzas they want, get the number with eval()
#eval()字符串变成数字
number_of_pizzas = eval( input("How many pizzas do you want: ") )
# Ask for the menu cost of each pizza
cost_per_pizza = eval( input("How much does each pizza cost: ") )
# Calculate the total cost of the pizzas as our subtotal
subtotal = number_of_pizzas * cost_per_pizza
# Calculate the sales tax owed, at 8% of the subtotal
tax_rate = 0.08 # we store 8% as the decimal value 0.08
sales_tax = subtotal * tax_rate
# Add the sales tax to the subtotal for the final total
total = subtotal + sales_tax
# Show the user the total amount due, including tax
print("The total cost is $", total)
print("This includes $", subtotal, "for the pizza and")
print("$", sales_tax, "in sales tax.")
字符串---Python中真正的字符
# SayMyName.py - prints a screen full of the user's name
# Ask the user for their name
name = input("What is your name? ")
# Print their name 100 times
for x in range(100):# Print their name followed by a space, not a new lineprint(name, end = " ")
sep, end, file and flush, if present, must be given as keyword arguments.
关键字参数:end
字符串用双引号或者单引号引起来
用字符串改进彩色螺旋线
# SpiralMyName.py - prints a colorful spiral of the user's nameimport turtle # Set up turtle graphicst = turtle.Pen()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue", "green"]# Ask the user's name using turtle's textinput pop-up windowyour_name = turtle.textinput("Enter your name", "What is your name?")# Draw a spiral of the name on the screen, written 100 timesfor x in range(100):t.pencolor(colors[x%4]) # Rotate through the four colorst.penup() # Don't draw the regular spiral linest.forward(x*4) # Just move the turtle on the screent.pendown() # Write the user's name, bigger each timet.write(your_name, font = ("Arial", int( (x + 4) / 4), "bold") )t.left(92) # Turn left, just as in our other spirals
文本对话框:turtle.textinput("Enter your name", "What is your name?")
列表---将所有内容放到一起
列表是一组值,用逗号隔开,放在方括号之间。
# ColorSpiralInput.py
import turtle # Set up turtle graphics
t = turtle.Pen()
turtle.bgcolor("black")
# Set up a list of any 8 valid Python color names
colors = ["red", "yellow", "blue", "green", "orange", "purple", "white", "gray"]
# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int(turtle.numinput("Number of sides","How many sides do you want (1-8)?", 4, 1, 8))
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):t.pencolor(colors[x % sides]) # Only use the right number of colorst.forward(x * 3 / sides + x) # Change the size to match number of sidest.left(360 / sides + 1) # Turn 360 degrees / number of sides, plus 1t.width(x * sides / 200) # Make the pen larger as it goes outward
数字对话框:int(turtle.numinput("Number of sides", "How many sides do you want (1-8)?", 4, 1, 8))
Python做作业
print("MathHomework.py")
# Ask the user to enter a math problem
problem = input("Enter a math problem, or 'q' to quit: ")
# Keep going until the user enters 'q' to quit
while (problem != "q"):# Show the problem, and the answer using eval()print("The answer to ", problem, "is:", eval(problem) )# Ask for another math problemproblem = input("Enter another math problem, or 'q' to quit: ")# This while loop will keep going until you enter 'q' to quit
整数除法://
eval():计算这样的表达式如:“5//2”
3.数据类型和变量---用Python做数学运算相关推荐
- python做数学计算器_从零开始学习PYTHON3讲义(二)把Python当做计算器
<从零开始PYTHON3>第二讲 上一讲我们说过了如何启动Python IDLE集成开发学习环境,macOS/Linux都可以在命令行执行idle3.Windows则从开始菜单中去寻找ID ...
- 用 Python 做数学建模
本文由 CDFMLR 原创,收录于个人主页 https://clownote.github.io,并同时发布到 CSDN.本人不保证 CSDN 排版正确,敬请访问 clownote 以获得良好的阅读体 ...
- FPGA开发技巧备忘录——verilog系统函数做数学运算
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 FPGA开发技巧备忘录--verilog系统函数做数学运算 前言 $clog2 Real math functions $random ...
- GEE:使用Sentinel-2数据做基于NDVI的长势监测(求5年影像集的NDVI均值,和当前年份的NDVI,两个影像做数学运算)
本文记录了农作物长势监测的原理和在GEE上实现的代码,记录了最简单的长势监测算法(更复杂的算法可以根据例子中的方法灵活转换) 详细代码:完整代码 https://code.earthengine.go ...
- 数学建模用python好吗_用 Python 做数学建模
数学建模中,大多数人都在用MATLAB,但MATLAB不是一门正统的计算机编程语言,而且速度慢还收费,最不能忍受的就是MATLAB编辑器不支持代码自动补全.python对于数学建模来说,是个非常好的选 ...
- shell脚本基础(环境变量、重定向、数学运算、退出脚本)
一.使用环境变量 1.环境变量 所谓环境变量,就是用来存储有关shell会话和工作环境信息的特性,这项特性允许你在内存中存储数据,以便程序或shell中运行的脚本能够轻松访问到它们.这也是存储持久数据 ...
- 面试问题-使用Java线程做数学运算
这是一个展示如何使用join()方法的例子. 问题: 使用Java多线程计算表达式1*2/(1+2)的值. 解决方案: 使用一个线程做加法运算,另一个线程做乘法运算,还有一个主线程main做除法运算. ...
- python之数学运算
一.python自带数学运算符 加:+ 减:- 乘:* 幂:** 除(非取整):/ 除(取整):// 除(求余):% i = 15 a = i/2 b = i//2 c = i%2 print(a,b ...
- [Robot Framework] 怎么做数学运算?
运用BuiltIn里面的Set Variable 转载于:https://www.cnblogs.com/MasterMonkInTemple/p/9020406.html
最新文章
- LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
- MySql UBUNTU下复制配置
- 一笔订单,但是误付了两笔钱!这种重复付款异常到底该如何解决?
- 《白帽子讲web安全》学习笔记 (3)
- java的 import注解_[java]一分钟学会spring注解之@Import注解
- su生成面域插件_插件玩的溜,SU不用愁
- python 操作微信闪电贷款_16、6个能够让Python程序快如闪电的小技巧
- 天梯赛 L2-011. (二叉树) 玩转二叉树
- 多线程android代码,android入门 — 多线程(一)(示例代码)
- python2与python3区别底层的区别_Python2 与 Python3 的区别(二)?
- 实力封装:Unity打包AssetBundle(三)
- 易语言怎么给手机发短信
- 通用接口测试用例设计
- 山西台达plc可编程控制器_PLC可编程控制器常见的应用领域
- 使用Overleaf写作是参考文献引用没按顺序
- 笔记本怎么自己装系统?u盘装系统windows7教程图解
- 图片转文字的实用方法
- 经济基础知识(中级)【8】
- 利用VBA筛选重复数据
- 无障碍建筑设计相关术语
热门文章
- java mvc tomcat_Java Servlet(七):JavaWeb MVC 操作(jdk7+tomcat7+eclipse)
- 【git】git 使用 Submodule 管理子模块 报错 pathspec did not match any files
- 【Elasticsearch】Elasticsearch:Searchable snapshot - 可搜索的快照
- 【Elasticsearch】使用Elasticsearch实现同段和同句搜索
- 【IDEA】Error:java: Compilation failed: internal java compiler error
- 【Flink】Flink消费Kafka数据时指定offset的五种方式
- 【Java】jstack报错Unable to open socket file: target process not responding or HotSpot VM not loaded
- 【Flink】Flink使用withParameters(Configuration)传参
- mac电脑LC_CTYPE: cannot change locale (UTF-8): No such file or directory
- java 连接solrcloud_Solr 14 - SolrJ操作SolrCloud集群 (Solr的Java API)