Python-Course1-Week1-Intro.
Python-Course1-Week1-Intro.
example. As simple as it is, this one line of code will ensure that we know how to print a
string in output and how to execute code within cells in a notebook.
# Try your first Python output
print('Hello, Python!')
After executing the cell above, you should see that Python prints Hello, Python!.
Congratulations on running your first Python code!
# Practice on writing comments
# Integer
11
# Float
2.14
# String
# Type of 12
type(12)
# Type of 2.14
type(2.14)
In the code cell below, use the type() function to check the object type of 12.0.
# Write your code below. Don't forget to press Shift+Enter to execute
the cell
type(12.0)
float
We can verify this is the case by using, you guessed it, the type() function:
# Print the type of -1
type(-1)
type(4)
type(0)
Once again, can test some examples with the type() function:
# Print the type of 1.0
type(0.5)
type(0.56)
sys.float_info
type(2)
# Convert 2 to a float
float(2)
type(float(2))
# Casting 1.1 to integer will result in loss of information
int(1.1)
int('1')
int('1 or 2 people')
float('1.2')
str(1)
str(1.2)
# Value true
True
# Value false
False
# Type of True
type(True)
# Type of False
type(False)
int(True)
# Convert 1 to boolean
bool(1)
# Convert 0 to boolean
bool(0)
# Convert True to float
float(True)
43 + 60 + 16 + 41
50 - 60
5 * 5
25 / 5
25 / 6
As seen in the quiz above, we can use the double slash for integer division, where the result
is rounded down to the nearest integer:
# Integer division operation expression
25 // 5
25 // 6
Let's write an expression that calculates how many hours there are in 160 minutes:
# Write your code below. Don't forget to press Shift+Enter to execute
the cell
30 + 2 * 60
And just like mathematics, expressions enclosed in parentheses have priority. So the
following multiplies 32 by 60.
# Mathematical expression
(30 + 2) * 60
x = 43 + 60 + 16 + 41
y = x / 60
y
x = x / 60
x
# Complicate expression