We can print words into our console:
print("hello world")
Note that quotes mark a string (a list of characters and letters):
"hello world"
We can also print integers (no quotes):
print(123)
We can also and floats (no quotes, with a decimal):
print(123.45)
And we can do math with these numbers:
print(1 + 2 * 3 / 4 - 5)
Sometimes, we want to store data for later use. We do this with variables:
a = 5
b = 6
c = "Hello World"
print(a + b)
print(c)
We can also take inputs from a user, and store them in a variable:
a = input()
print(a)
We can convert strings to numbers, and numbers to strings with the int()
and str()
functions:
a = int("123")
b = str(123)
print(a)
print(b)
- Must ask user for input (as in the computer prints: “Please enter width:”)
- The area of a triangle is (width * height) / 2
- Use the int() function to convert input to integer
w = int(input("Please enter the width: "))
h = int(input("Please enter the height: "))
print((w * h) / 2)
x = 1
y = x
x = 3
print(x + y)
a = 1
a = 2
print(a)
four = 4
five = 5
print(four + five)
x = 10
y = 6
y = y + 2
z = x - y
print(z)
my_variable = '4'
my_variable = 4
my_variable = my_variable * 4
print(my_variable)