WK1 - Python - Basics - Part1: 1 Introduction To Programming in Python
WK1 - Python - Basics - Part1: 1 Introduction To Programming in Python
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
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'
In [1]: type(n)
1
---------------------------------------------------------------------------
<ipython-input-1-74adfa83daed> in <module>()
----> 1 type(n)
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.
In [12]: n+3
Out[12]: 12
In [13]: s.upper()
Out[13]: 'YELLOW'
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).