Basic programming
- All code is entered, use text input
Write a simple program
- Circle area formula: area = radius * radius * 3.1415
Not need to define the data type. Python
Read console input
- is input into the input string
- eval
- In jupyter using shift + tab key to jump interpreting the document
Variable naming specification
- Consists of letters, numbers, underscores
- You can not start with a number *
- Identifiers can not be keywords (actually can force change, but for coding standard is extremely unsuitable)
- It may be any length
- Camel name
Variable assignment statements and assignment expressions
- Variables: Popular understood to amount may vary
- x = 2 * x + 1 is a mathematical equation, and in that it is an expression language
- test = test + 1 * variables must have a value before assigning
Meanwhile assignment
var1, var2,var3... = exp1,exp2,exp3...
Define constants
- Constant: it shows a value identifier, suitable for multiple use scenario. For example, PI
- Note: If you define constants, so the constant is low can not be changed in the other language, but in Python everything is an object, but also the constant can be changed
Numeric data types and operators
- There are two types of values (int and float) suitable for addition, subtraction, die in Python, power of
Operator /, //, **
Operator%
EP:
- 25/4 number, if you want to convert it to an integer how to rewrite
- Enter a number is odd or even is determined
- Advanced: Enter a number of seconds, to write a program to convert and second components: for example, 500 seconds is equal to 8 minutes and 20 seconds
- Advanced: If today is Saturday, then after 10 days of the week? Tip: Every week day 0 Sunday
Scientific notation
- 1.234e + 2
- 1.234e-2
And calculating a priority calculation expression
Enhanced assignment operator
Type Conversion
- float -> int
- Rounding round
EP:
- If a sales tax of 0.06%, the annual income for 197.55e + 2, and how much to pay tax? (2 result retains a decimal)
- You must use scientific notation
Project
- Written in Python and a loan calculator program: input is a monthly (monthlyPayment) output is the total number of payments (totalpayment
Homework
1
In [2]
celsius = float(input("Enter a degree in Celsius:"))
fahrenheit = ( 9 / 5) * celsius + 32
print('{} Celsius is {} Fashrenheit'.format(celsius,fahrenheit))
- 2
In [1]:
import math
radius = float(input('Enter the radius :'))
length = float(input('Enter the length :'))
area = radius * radius * math.pi
volume = area * length
print('The area is:%.4f' % area)
print('The volume is:%.1f' % volume)
- 3
-
In [8]:
feet = float(input('Enter a value for feet :'))
meters =(305 / 1000) * feet
print('{} feet is {:.4f} meters:'.format(feet, meters))
- 4
-
In [10]:
import math
M = float(input('Enter the amount of in kilograms :'))
initialTemperature = float(input('Enter the initial temperature :'))
finalTemperature = float(input('Enter the final temperature :'))
Q = M * (finalTemperature - initialTemperature) * 4184
print('The energy needed is:%.1f' % Q)
- 5
-
In [1]:
import math
balance,interstrate = map(float,input('Enter balance and interest rate (e.g., 3 for 3%):').split(','))
interest = balance * (interstrate / 1200)
print('The interest is:%.5f' % interest)
- 6
-
In [2]:
import math
v0,v1,t = map(float,input('Enter v0, v1, and t:').split(','))
a = (v1 - v0 ) / t
print('The average acceleration is:%.4f' % a)
- 7 进阶
-
In [11]:
import math
A = float(input("Enter the monthly saving amount:"))
B=A * ( 1 + 0.00417)
C=(A + B) * ( 1 + 0.00417)
D=(A + C) * ( 1 + 0.00417)
E=(A + D) * ( 1 + 0.00417)
F=(A + E) * ( 1 + 0.00417)
G=(A + F) * ( 1 + 0.00417)
print('After the sixth month, the account value is :%.2f' %G)
- 8 进阶
In [6]:
number = int(input("Enter a number between 0 and 1000:"))
a = number // 100
b = number // 10 % 10
c = number % 10
print('The sum of the digits is',a+b+c, end = ' ')