Files
campus-fryslan-introduction…/Week-2/00. Simple Expressions.ipynb
T
2018-09-14 17:33:07 +02:00

6.8 KiB

This exercise shows you how to work with variables and expressions, Let's start, press CTRL+ENTER to run the cell.

Everytimes there is a None, you need to change that to the correct expression.

PS: this is exercise 0, because computers start counting at 0 instead of 1.

In [ ]:
x = 13
print('x: ', x)
print('type(x): ', type(x))

Excercise: Apply integer division by 2 on x using //, replace None with the correct expression.

In [ ]:
x = None
print('x: ', x)
print('type(x): ', type(x))

Notice that x is still an integer

Excercise: Apply floating point division by 2 on x using /

In [ ]:
None
print('x: ', x)
print('type(x): ', type(x))

Notice that x is a floating point number now, even though 6 is divisible by 2.

Excercise: Create a new variable y, with value 9

In [ ]:
None
print('x: ', x, ', y: ', y)
print('type(x): ', type(x), ', type(y): ', type(y))

Notice that x is still a float, and y an integer.

Exercise: Assign the sum of x and y to y

In [ ]:
None
print('x: ', x, ', y: ', y)
print('type(x): ', type(x), ', type(y): ', type(y))

Notice that x did not change, and that y becames a float.

Exercise: Square y using **

In [ ]:
None
print('x: ', x, ', y: ', y)
print('type(x): ', type(x), ', type(y): ', type(y))

Now, let's work with strings.

Exercise: Assign the text value 'https://www.rug.nl/' to the variable url.

In [ ]:
None
print('url: ', url, ', type(url): ', type(url))

Exercise: Use + and * to add 10 asterisks ('*') to the url

In [ ]:
None
print('url: ', url, ', type(url): ', type(url))

Exercise: Use url.find( ... ) to locate the first asterisk, assign that position to asterisk_at

In [ ]:
None
print('url: ', url, ', type(url): ', type(url))
print('asterisk_at: ', asterisk_at, ', type(asterisk_at): ', type(asterisk_at))

Exercise: Use [: ... ] to extract the first asterisk_at characters of url, removing the asterisks

In [ ]:
url = url[: None]
print('url: ', url, ', type(url): ', type(url))

Not everything can be solved by multiplication, addition and such operations, even in mathematics.

Even though this is not entirely true, let us assume it is for practical purposes.

Some functionality comes as a function. One example of such a function is print. You use this function with brackets.

Exercise: get the type of print using type( ... ).

In [ ]:
type(None)

The syntax for calling functions is similar to that of mathematical functions, like sine and cosine. Python also has those.

In [ ]:
from math import sin, cos, sqrt

Exercise: calculate the sine and cosine of 0, and the square root of 16.

In [ ]:
cosine_at_0 = None
sine_at_0 = None
sqrt_of_16 = None

print('cosine at 0: ', cosine_at_0)
print('sine at 0: ', sine_at_0)
print('square root of 16: ', sqrt_of_16)

You've learned about

  • Expressions: x * (5 + y)
  • Assigments: x = 5
  • Strings: 'Mark ate a burger yesterday'
  • Functions: print('hello'), sin( ... )
  • Types: type( ... )