forked from a-newman/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3_arithmetic.py
More file actions
77 lines (55 loc) · 1.84 KB
/
3_arithmetic.py
File metadata and controls
77 lines (55 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# ** Math operations **
#
# Python supports many different arithmatic operations on integers and floats!
# The result of an arithmetic operation is another number.
x = 10 # x is an integer
y = 5.0 # y is a float
print("x=", x)
print("y=", y)
print("\n") # this prints a new line
print("Examples of math operations:")
# Addition:
print("x+y=", x + y)
# Subtraction:
print("x-y=", x-y)
# Multiplication: note that multiplication is done using an asterisk *:
print("x*y=", x*y)
# Division:
print("x/y=", x/y)
# Exponentiation (taking a number to a power)
# Note that exponentiation uses a double asterisk **
print("x**2=", x**2)
# Taking the remainder
# Use the `%` operator to get the remainder of a division operation
# This is also known as the "modulo" operator or "modding"
print("x % y=", x%y)
print("3 % 2 =", 3 % 2)
print("3 % 4 =", 3 % 4)
print("\n")
# ** Grouping **
print("Examples of grouping")
# You can use parentheses to group operations!
print("2+2/2 =", 2+2/2)
print("(2+2)/2=", (2+2)/2)
print("\n")
# ** String operations **
# You can use some arithmetic operations on strings too!
print("Examples of string operations")
# Adding two strings combines them.
print("I love " + "programming in Python!")
# Multiplying a string by an integer n repeats that string n times
print("Python is great! " * 3)
# Be careful: other arithmetic applied to strings will throw an error!
# ** Exercises **
print("\n")
print("Output of exercises")
# 1. Modify the line below so that variable a is equal to 101 plus 3 to the 4th
# power, all divided by 4.
a = 0
print("Should equal 45.5:", a)
# 2. What happens when you try to divide by zero in Python? Uncomment and run
# the following line.
#print("Dividing by zero:", 1/0)
# 3. Write code to calculate and print out the remainder of the division
# operation 18932/328
# Your code here