You are on page 1of 2

WK1_Python_Basics_Part1

November 23, 2019

1 Introduction to Programming in Python


Hello world!
Run the code in the cell below by selecting it and hitting Shift+Enter.

In [5]: 3+5

Out[5]: 8

2 Python Basics
2.1 Basic data structures
Let’s first create a numerical variable (of type integer) called ‘n’.

In [10]: n=9

Let’s print ‘n’.

In [11]: print(n)

Now let’s create a string variable called ‘s’, and return it (rather than print it).
Note that you can include more than one lines of code in a cell.
Also note that code lines starting with the hash symbol # are comments.

In [8]: # This is a comment (code lines starting with '#' are comments)
s='yellow'
s

Out[8]: 'yellow'

Ask for the type of ‘n’.

In [1]: type(n)

1
---------------------------------------------------------------------------

NameError Traceback (most recent call last)

<ipython-input-1-74adfa83daed> in <module>()
----> 1 type(n)

NameError: name 'n' is not defined

Small side note: type( ) and print( ) are examples of functions in Python. This means that
someone has specified what they are meant to do when we use them. We’ll be using lots of
different handy Python functions in this course. This makes our life easier, as this way we don’t
need to specify all functionality from scratch. Note that we could also specify our own functions
in Python, if we wanted to - we won’t learn how to do this in this course, so as to keep things
simple.

2.1.1 Use the variables created


Add 3 to ‘n’.

In [12]: n+3

Out[12]: 12

Make ‘s’ uppercase.

In [13]: s.upper()

Out[13]: 'YELLOW'

Concatenate ‘s’ with " submarine".

In [15]: s+' submarine'

Out[15]: 'yellow submarine'

Overwrite the value of ‘n’ and print it.

In [14]: n=10
print(n)

10

Print out information in a user-friendly form (e.g. by combining strings and the values of
variables).

You might also like