First Python programs

Introduction

It is not possible to go through Python programming in just an hour. We will therefore learn only those features that are absolutely needed for the exercises. If you want to know a bit more, please go through Python tutorial. It is worth the effort!

Assignments and some simple arithmetic

a=b+c is not a mathematical equation in Python. It says that the sum of the b and c values is assigned to the value a. In the example below, a gets the value 5, b gets the value 3. plus, minus, mult and div are variables with take the sum, the difference, the product and the division of 5 and 3. As you can see, a and b are integer values. The same is true for plus, minus and mult as addition, subtraction and multiplication of integer values yield integer values again. The situation is different for division, and Python automatically assigns a float value to div. If you insist on an integer division (which would yield 1) you should use: div = a // b.

After you typed the program, please save it in a file on the PC (file ->save as). Then press the green button to run it.

assignments.png

Variable types

Variable types are generally assigned automatically by Python.

a=5           # will assign the integer value 5 to the variable a. The # sign demotes a comment.
a=5.0         # will also assign the value 5 to the variable a, but this time it is the floating point value
b = True      # or False assigns a boolean (logic) value to the variable b. 
s= "Hello"    # assigns the text string Hello to the variable s
l = [1,2,3,4] # l is a list of numbers. You can access the list elements through an index starting from 0. So... l[2] will return 3
t = (1,2,3,4) # t is a tuple. Tuples are similar to lists, but they are immutable. You cannot change the values of their elements

Conditions

In programming, you must often execute some code only if a condition is true and sometimes execute another code if not.

A typical conditional Python statement looks like this:

a=5
b=3
if a == 3:
    print("a and b are equal")
    print("They are still equal")
elif a < b:
    print("a is smaller than b")
else:
    print("a is bigger than c")

Comparison operators are:

  • < smaller than, <= smaller or equal
  • > bigger than, >= bigger or equal
  • == equal # be careful here. A typical error is the omission of the second "=" in which case you get an assignment instead of a condition
  • = not equal
As you can see, the body of the condition is indented. All indented statements following the condition will be executed if the condition evaluates to True.

Loops

The computer is particularly good at doing repetitive task, tasks humans find boring. This is done with loops. We may want the computer to permanently check the state of a signal and only warn is if the signal has changed.

There are different types of loops. The ones we will use are the while loop which runs as long as a certain condition is true and a for loop, which runs a predetermined number of times. Again the statements of the loop body are indented. Of course, you can create a loop with a loop, in which case the loop header of the inner loop will be indented once while the body of the inner loop will be indented twice.

while True:
    print("Hello")     # This will print Hello forever

a = 5
while a > 0:
    print("Hello")
    a = a - 1          # Decrement a by 1, this can be abbreviated by: a -= 1
                       # The same holds for increment: a = a + 1 or a += 1

Question: How often will Hello be printed?

for i in range(5):          # range(x) creates a sequence of numbers starting from 0 and going up to, but not including x
    print("Hello")

I guess, we are ready for some experiments ExerciseSheets#FirstSession

After that we will see how to use Python modules, how to control some timing and how to write your own functions in Modules and Timing

-- Uli Raich - 2022-10-14

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng assignments.png r1 manage 127.4 K 2022-10-14 - 19:03 UliRaich  

This topic: AFNOG > ProgrammingPython > FirstPythonPrograms
Topic revision: r4 - 2022-10-20 - UliRaich
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback