COMP3207 High-level general purpose programming language
High-level general purpose programming language
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Python - Concepts
COMP3207
2Overview
• High-level general purpose programming language
• 30 years of age
• Part of Top-3 most popular languages
– The other two are Java and JavaScript ;)
• Latest version is Python 3.10 (last week)
– The material in this lecture is for Python 3.6 and higher
(minimum currently supported by Azure)
– You shouldn’t use <3.6 unless you maintain a legacy system.
3The Zen of Python (fragment)
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Full list at PEP-020
4Replit
• In this lecture, we switch between slides and Replit, a Cloud-
based IDE that uses REPL
• REPL = Read-Eval-Print. A way to execute code online for
pedagogical purposes
(and that I can run from the lectern PC!)
• While we use it, think about how it could work and how to
implement it on the Cloud
• You should install Python locally and use the environment
you feel most comfortable with.
5Python uses interpreter
Let’s look at Python’s interpreter on replit
6Interpreters and compilers are tools
• There is no Interpreted/Compiled language
• There are Interpreters/Compilers for languages
• You can have a compiler for Python or an Interpreter for Java
– Replit implements a Java interpreter
– IDEs use a compiler to provide hints and suggestions for Python
• There is also Just-in-Time compilation, more info here
7Python syntax quirk #1 - Indentation
• Indentation makes blocks of code
– Not with braces (like Java)
Source: AskPython
8Python syntax quirk #1 – Why?
• Encourages more
readable code
• Discourages deeply
nested code
• Harder to write without
IDE help
• Copy/Paste nightmare if
mixing whitespaces,
tabs and newlines.
Pros: Cons:
Let’s look at this on replit
9Semantic Quirk #1 – Weak Type Safety
Strong type safety
Types of variables and arguments of functions:
Need to be explicitly declared
Are statically checked
(at compile time)
Are dynamically checked
(at runtime)
Don’t need to be declared
Weak type safety
10
Type Safety – Pros and cons
Strong type safety
Types of variables and arguments of functions
Protects you from yourself Let you crash
Weak type safety
Slower development cycle
More code needs to be written
Faster development cycle
Read post by Erik Engheim for more on Static vs Dynamic typing
More tests need to be written
11
Basic types and classes
• Int, float, Boolean are types like in Java
• list and set
– No need of declaring the type of the elements
• String is an immutable list with some extra methods
• Dictionary dict
– In Java this is an Abstract Class
• No implementations included (LinkedList, ArrayList)
Let’s look at this on replit
12
Lists
• “Flat Sequences”: For holding primitive values efficiently
– String, bytes, array.array
– Array.array has type constraints.
• “Containers”: mixed type and nested value. Flexibility for
faster coding
– List, set, tuple (immutable list)
13
Source: Fluent Python. Luciano Ramalho 2021 (O’Reilly)
14
Variables are not boxes!
Source: Fluent Python. Luciano Ramalho 2021 (O’Reilly)
Watch out when you do a = b !
Let’s look at this on replit
15
Classes
• Python is fully Object-Oriented language
• Almost all you know from Java applies except:
– Syntax and conventions for getters and setters
– No true access modifiers (public, protected and private)
– Multiple class inheritance is possible
• The concept is closer to C++.
16
Access modifier conventions
• Prepended underscore: “protected”
– _method()
• Two prepended underscores: “private”
– __method()
17
Interfaces
• Reminder: Group of related methods that define a contract
that a class needs to implement
• In Java
– Explicit declaration of implementation
– Compiler checks you did implement all methods
• In Python
– Built-in classes have “magic methods” that you can override
• Is like you implement the “interface” of the built-in class
– Informal. No checking at all
18
Source: Fluent Python. Luciano Ramalho 2021 (O’Reilly)
19
Magic methods
• If class Cla overrides __len__, __contains__ and __iter__, then
for instance c you get:
– Len(c)
– A in c
– For item in c
• You can also override operators like +, - and * and __str__
(toString)
YOUR QUESTIONS