Resource of lean Python Variable
- https://www.learnpython.org/en/Variables_and_Types
- https://www.w3schools.com/python/python_variables.asp
Math is the basic function of the Python, please try below code and learn the basic operation of "+,-,*, /, %"
x = 3
y = 4
answerPlus = x+y
answerMinus = x-y
answerMultiple = x*y
answerDivide = x/y
answerWhatEver = (x*y)+(x-y)/x
answerMode=x%y
print(x) #3
print(y) #4
print(answerPlus) #7
print(answerMinus) #-1
print(answerMultiple) #12
print(answerDivide) #0.75
print(answerWhatEver) #11.666666666666666
print(answerMode) #3
Use print()
function you could print the result, but python can't print mix the data type of number and a string.
Below is working
x = 3
print(x)
But this is not working:
print("x="+x)
You will got error like below:
To correct the type error, you need convert the number to string first.
print("x=" + str(x))
Assign a expression to a variable
x = 3
y= x+2
example of chain assignment
a = b = x*(y+x)
Check the results:
print("a="+ str(a))
print("b="+ str(b))
please try yourself and print the result.
There are more math operators you may need, pleas try them and see how it works.
-
use python get the answer of below express 3 + 8 × 9 ÷ 7 × ( 5 +6 )
-
Update your previous turtle drawing project, using some of math to change the graphic pattern
All variable has a type in python, for example, the string "abc" has a type "String".
The number 1,2,3 is integer number, but 1.23 is fraction number.
In python, the integer number type is int
the fraction number type is float
You could use type(variable)
to get the type of the variable
number = 9
print (type(number))
float_number = 9.0
print(type(float_number))
The type of number could convert each other:
number = 9
f_number= float(number)
print(number)
print(f_fumber)
please run it ans see the result yourself.
Augmented assignment is the combination, in a single statement, of a binary operation and an assignment statement:
Try below code to see the result
number = 9.0
print("number = " + str(number))
number -= 2
print("number = " + str(number))
number += 5
print("number = " + str(number))
The Boolean Type only have two value: true
and false
You could compare two value and see the result:
- To check if two variable value is same:
a = 2
b = 3
c = 2
result1 = (a == b)
print(result1) # false
result2 = (a == c) # true
print(result2) # true
There is more operation will got boolean result:
Logic operators are used to combine the two boolean statements:
Operator | Description | Example |
---|---|---|
and | Return true if both are true | x<4 and x>1 |
or | Return True if one of the value is true | x<4 or x>10 |
not | reverse the result | not(x>y) |
-
Write a Python program, given a radius value, it compute and area of the circle. example: r = 1.1 Area = 3.8013271108436504
-
Please use python print all value between 1 and 10, with the condition:
- x>1 and x<5
-
Goto the webpage (basic math practice) and complete the exercises #4, #10, #38, #48,, #119
-
MiniProject
- Create a Python project to get the value of Pi to n number of decimal places. Note: Input a number and the program will generate PI to the 'nth digit
Click me to see the sample solution
- Create a Python project to guess a number that has randomly selected.