pm21-dragon/lectures/lecture-01/4 - Python programming basics.ipynb
2024-10-25 06:36:44 +02:00

13 KiB

None <html> <head> </head>

Python programming basics

Here are some basic examples of concepts that we will learn in the class. We will also run these at http://pythontutor.com/visualize.html

Built-in names

Python has several words which are "built in" and do special things. When we start, we will use a lot of these. As we get further along, we will start using our own names and names we imported more and more.

In [1]:
print("hello world")
hello world

Variables

Another building block of programming are variables. These are names given to hold values. Each variable has a type, such as a string (like "hello" -- note the quotation marks) or integer (like 42).

In [3]:
x="hello world"
In [5]:
print(x)
hello world
In [7]:
print("x")
x
In [9]:
x
Out[9]:
'hello world'
In [11]:
type(x)
Out[11]:
str
In [33]:
x=42
In [35]:
print(x)
42
In [37]:
print(42)
42
In [39]:
x
Out[39]:
42
In [41]:
type(x)
Out[41]:
int
In [43]:
print(print)
<built-in function print>
In [45]:
print
Out[45]:
<function print(*args, sep=' ', end='\n', file=None, flush=False)>
In [47]:
type(print)
Out[47]:
builtin_function_or_method
In [49]:
print(type)
<class 'type'>
In [51]:
type
Out[51]:
type
In [53]:
type(type)
Out[53]:
type
In [55]:
type(type(x))
Out[55]:
type

Assignment

The process of setting a variable is called assignment. This is done with the equals (=) symbol.

In [57]:
x = "my new string"
In [59]:
x
Out[59]:
'my new string'
In [61]:
x = -1
In [63]:
x
Out[63]:
-1
In [65]:
x = x + 1
In [67]:
x
Out[67]:
0

Let's look at these in the Python Tutor...

Functions

A building block of programming are functions. These are pieces of code that are called (also known as executed) with input and return output. They may also have side-effects.

Let's look at a function definition:

def my_function_name(argument1, argument2):
    variable1 = argument1 + argument2
    print("hello from my function") # this is a "side-effect"
    return variable1

Above, we created a function named my_function_name. This function takes two inputs (called arguments). There are several steps performed in the function. First, we add argument1 to argument2 and store the result in variable1. Then we print something. Then we return variable1 as the output of our function.

The indentation (spaces) at the beginning of the lines in the function tell Python which lines of code are part of the function. Everything with the same level if indentation is a "block".

Above, we called the built in function print().

The print() function takes any number of arguments as input, returns nothing, and as a side-effect prints the arguments to the screen.

Errors ("exceptions")

Python errors are called "exceptions". An important part of programming is figuring out why you got a particular error. Read the error message very carefully - it contains type of error, a description of the error, and a "traceback" that shows where the error came from.

In [69]:
1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In[69], line 1
----> 1 1/0

ZeroDivisionError: division by zero
In [71]:
x = variable_does_not_exist + 4
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[71], line 1
----> 1 x = variable_does_not_exist + 4

NameError: name 'variable_does_not_exist' is not defined
In [85]:
defx asdlkfj39:
    adsk..{
  Cell In[85], line 1
    defx asdlkfj39:
         ^
SyntaxError: invalid syntax
In [81]:
def asdlkfj39():
    adsk
In [83]:
asdlkfj39()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[83], line 1
----> 1 asdlkfj39()

Cell In[81], line 2, in asdlkfj39()
      1 def asdlkfj39():
----> 2     adsk

NameError: name 'adsk' is not defined

Doing the assignments

We use automatic tests in this course. This way you can check most of your own work. Below is an example of how this works.

Example Problem

Assign a value of 2 to the variable named x.

In [87]:
# Write your answer here
x = 2
In [89]:
assert x==2
In [ ]:
 
</html>