These exercises are about data types from Session 1 session1.
Exercise 1 - Functions
- Look up what the
pow() function does and how it works.
Try to do this in python.
## Help on built-in function pow in module builtins:
##
## pow(base, exp, mod=None)
## Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
##
## Some types, such as ints, are able to use a more efficient algorithm when
## invoked using the three argument form.
- Use the
pow() function to calculate 4^13.3
## 101718016.92449336
- What data type is 4^13.3? Coerce it into an integer. Then a
string.
myfloat = pow(4,13.3)
type(myfloat)
## <class 'float'>
myint = int(myfloat)
type(myint)
## <class 'int'>
## 101718016
## <class 'int'>
Exercise 2 - Variables
- Create a variable named “myfirst_var” and store a number in it.
- Print the variable with the print() statement
myfirst_var = 1
print(myfirst_var)
## 1
- Now let’s do a mathematical operation within a variable called
“operation_var”; let’s try 2+2. Print the result.
operation_var = 2+2
print(operation_var)
## 4
Notice how you can perform math operations within a
variable.
Now let’s create a third variable. Start the name of the variable
with a number, followed by an underscore and the word “var”. Store a
random number in it.
## File "<string>", line 1
## 1_var = 1
## ^
## SyntaxError: invalid decimal literal
## File "<string>", line 1
## print(1_var)
## ^
## SyntaxError: invalid decimal literal
- Notice how you obtain a syntax error? Now let’s try this again:
create a new variable, this time starting with the word “var”,
underscore, and a number. Store a random number in it and print your
variable.
## 1
- This naming convention is acceptable. Now create a different
variable containing symbols (e.g. @&%^). Store a random number in
it.
## File "<string>", line 1
## var_% = 1
## ^
## SyntaxError: invalid decimal literal
## File "<string>", line 1
## print(var_%)
## ^
## SyntaxError: invalid syntax
- Once more, notice how you obtain a syntax error. Symbols are not
accepted when naming a variable.
Exercise 3 - Coercion
- Now let’s create a variable called “coerce_var” and store the word
“hello” in it.
- Check the type of coerce_var with type()
- Let’s try coercing it into a numeric integer with int(). What
happens if you do this?
coerce_var = "hello"
type(coerce_var)
int(coerce_var)
## <class 'str'>
## invalid literal for int() with base 10: 'hello'
- Notice how you get an error when attempting this; you cannot convert
“hello” into an integer. Let’s try converting coerce_var into a bool()
now
## True
- coerce_var has been converted to a boolean type variable.
- Therefore, you can convert strings of characters into boolean types
but not numeric types.
- Let’s try vice versa: create a variable called “coerce_num” and
store a number in it. Coerce it into a string with str()
coerce_num = 3
str(coerce_num)
## '3'
- Notice how there is no error this time. It is possible to coerce a
numeric type into a string.
- Let’s try storing a number as a string (i.e. with quotations), and
changing its type to integer.
coerce_var = "3"
type(coerce_var)
## <class 'str'>
## 3
## <class 'int'>
- This is possible given that numeric characters do have an integer
equivalent.
Exercise 4 - Concatenation
- From the methods learned in class, try concatenating the strings “I”
” code” in 3 different ways. First try to concatenate both strings
within one variable. Print the results from your different
attempts.
concatenated_str = "I" " code"
print(concatenated_str)
## I code
concatenated_str = "I" + " code"
print(concatenated_str)
## I code
concatenated_str = "I"
concatenated_str += " code"
print(concatenated_str)
## I code
- Then try creating 1 variable called “concatenate_1” which stores “I”
and another called “concatenate_2” which stores ” code” and concatenate
both variables. Try, for example, print(concatenate_1 + concatenate_2) -
what do you get?
concatenate_1 = "I"
concatenate_2 = " code"
print(concatenate_1 + concatenate_2)
## I code