PROGRAMMING IN
PYTHON
FERRY KEMPERMAN
NANJING FOREIGN LANGUAGE SCHOOL
JANUARY 2019
HIGH LEVEL PROGRAMMING LANGUAGES
• Pseudocode and flowcharts are techniques to design algorithms.
• Technically they are part of your ‘software design’
• A programmer in any language should be able to implement your design
• In software engineering, implementation means writing high level statements (code)
to be able to execute your program
• C++, C#, Visual [Link], Java, Python, Pascal, Delphi and so on are
• examples of different high level languages.
• They differ not only in syntax, but way more important…..
• in programming paradigms
PROGRAMMING PARADIGMS
• Programming languages have different foundations.
• All languages have mathematical computation as a base.
• The style in which you program depends on this foundation, called a paradigm
• There are two main paradigms: declarative and imperative languages
• Declarative programming = Telling the computer what needs to happen (what)
• Imperative programming = Telling the computer what it should do (how)
• Within these paradigms you have subdivisions like
• Functional programming, logic programming, procedural programming, object
oriented programming and so on
WHAT IS PYTHON ?
• High level language founded by fellow Dutchman Guido van Rossum in the late
eighties
• Python 2.0 (2000) and Python 3.0 (2008) were the major version releases since then.
• Python supports multi programming paradigms, which makes it ideal for learners
• Python’s syntax is relatively simple and straight forward
• We will use Python throughout our A-level course this year and next year as the
preferred high level language.
• Feel free to use other high level languages like C and Visual Basic on the side if you
wish as long as you write your implementation in Python too.
PYTHON: PRACTICAL
• Schoology contains the download link for Python, tutorials and FAQ.
• Python statements can written in a CLI called Python Shell. One
statement at a time.
• For programming in Python we use an IDE, an Integrated Development
Environment. There are several IDE’s available for Python. We will use
Wing IDE, a dedicated Python development interface.
• Download link for Wing IDE is provided on Schoology as well.
PYTHON SYNTAX
• Python doesn’t need variable declaration and initialization
• Python uses dynamic data typing: once you use a variable and assign a value it
instantly assigns the right data type to the ‘drawer’ based on the assigned value.
• However: the exam and your teacher requires you write down your variable
declaration as comments in your Python code (!!)
• Every statement must be on a separate line
• Keywords are written in lower case
• Variable names are case sensitive: Counter and counter are two different variables
• Python programs are not compiled, but interpreted
• Constants are written in all upper case letter: PI = 3.14 for example
WING IDE, THE INTERFACE
Run/Debug
Python files have the .py
extension. Save files into project
directory
Python statements.
Colors show how the interpretator
‘reads’ the code.
Start with # for comments (green,
ignored by interpretator)
String (purple) between quotes
Numerical values (blue), no quotes
Variable/Constant names in black
WING IDE: THE INTERFACE
I/O screen in Wing IDE will
show all print and input
statements. Interaction with
user, like in this example,
will take place here too.
PSEUDOCODE VS PYTHON SYNTAX
Pseudocode Python
OUTPUT “My name is Joey” print(“My name is Joey”)
OUTPUT “My name is” + Name print(“My name is”, Name) or print(“My name is “ + Name)
OUTPUT “My name is “ + Name + “ and I am” + Age + “ years print(“My Name is”, Name, “and I am”, Age, “years old.”)
old” → Use print(“My Name is”, Name, end=‘’) for next line
INPUT “Enter your name: “ Name Name = input(“Enter your name: “)
IF Counter < 0 if Counter < 0:
THEN OUTPUT “Computer Science is fun” print(“Computer Science is fun”)
ELSE OUTPUT “Math is fun” else:
ENDIF print(“Math is fun”)
IF..THEN..ELSE.. IN PYTHON
• Python uses indentation to format COF structures like IF-THEN-ELSE
• In Python you only write if..else…, keyword then is not allowed
• The semicolon : after the Boolean expression serves als ‘then’
• Indentation = alignment of statements to determine its position in code
FOR – LOOP IN PYTHON
PSEUDOCODE PYTHON
FOR Counter Start To End For Counter in range(Start,End):
OUTPUT Counter print(Counter)
ENDFOR
WHILE – LOOP IN PYTHON
PSEUDOCODE PYTHON
DECLARE Counter : INTEGER #DECLARE Counter: INTEGER
Counter 0 Counter = 0
WHILE Counter < 3 while Counter < 3:
OUTPUT Counter print(Counter)
Counter Counter + 1 Counter = Counter + 1
ENDWHILE
GOOD TO KNOW BEFORE WE GET STARTED
• for, if, else, while etc. are all keywords that should be written in lower
case at all times.
• UNTIL loops don’t exist in Python, but use WHILE loops instead.
• The equal sign (=) in Python is ==, not equal is != in Python.
• If Counter == 3 (Counter equals three)
• If Counter != 3 (Counter not equal to three)
• In Pseudocode we use for variable assignment, in Python
assignment uses the equal sign =
• Counter 3 in Pseudocode (storing 3 in the variable Counter)
• Counter = 3 in Python (storing 3 in the variable Counter)