robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
A SMALL INTRODUCTION TO PYTHON
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o General Information
o Installation and IDEs
o The Python Interpreter
WHAT WE WILL DISCUSS
PART 1:BASICS
PART 2:OBJECT-ORIENTED PROGRAMMING
o Syntax
o General Idea of OOP
o Classes
o Demonstration
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
PART 1
BASICS
LET‘S START WITH…
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Interpreted, dynamically typed language
o Widely used in web development, data science, machine learning
o Slower but more forgiving
o Easy to learn
o Enables rapid prototyping for algorithms
o This course uses python3.8+
PYTHON : GENERAL INFORMATION
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
INSTALLATION
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Write code with the editor of your preference
o Code is stored in .py files
o Code can be executed by calling script files or by invoking interactive mode
HOW IS CODE WRITTEN, STORED AND EXECUTED?
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
PyCharm by IntelliJ
Visual Studio Code
IDE / EDITORS – SOME RECOMMENDATIONS
https://www.jetbrains.com/pycharm/
https://code.visualstudio.com
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
THE FIRST PROGRAM – HELLO WORLD!
Listing 1: hello_world.py
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Interactive mode
o REPL (read-evaluate-print loop)
o Hence the term „interpreted“
o Can be used to test small snippets of code
THE PYTHON INTERPRETER
https://docs.python.org/3/tutorial/interpreter.html
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
THE PYTHON INTERPRETER
https://docs.python.org/3/tutorial/interpreter.html
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
WORKSHOPDON‘T HESITATE
TO ASK QUESTIONS!
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
PyCharm by IntelliJ
Visual Studio Code
https://www.jetbrains.com/pycharm/
https://code.visualstudio.com
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
PART 2
ADVANCED CONCEPTS
LET‘S MOVE ON TO…
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
SYNTAX
DETAILS ABOUT THE…
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o No braces, no semicolons at end of line
o Comments start with #
o Level of indentation determines scope of instructions
o Short and concise keywords
o Very close to natural language
PYTHON – SYNTAX & STRUCTURE
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
VARIABLES & COMMENTS - EXAMPLE
Listing 2: variables.py
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
FUNCTIONS & CONDITIONAL FLOW
Listing 3: beers.py
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
BLOCKS – INDENTATIONS DETERMINE INSTRUCTION SCOPE
Listing 4: beers.py
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
OBJECT-ORIENTEDPROGRAMMING
WHAT IS…?
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Programming paradigm based on the concept of objects
o Intends to map real world objects in software
o Objects contain data in the form of fields / attributes and methods / functions
o An Object’s function can access and modify its data fields
o Objects have a notion of "this" or "self"
OBJECT-ORIENTED PROGRAMMING (OOP)
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Classes define structure of objects
o Provide a means of bundling data and functionality together
o Represent the “blueprint” of a new type of object
o Attributes attached to a class represent state
o Functions attached to a class represent capabilities / contracts
o Class instantiations are called objects
CLASSES
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
OBJECT-ORIENTED PROGRAMMING (OOP)
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
DEMONSTRATIONOF OOP
A LITTLE…
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
o Python is a very powerful, beautiful language
o Python offers functions, variables, classes, inheritance, modules and a lot of different data structures from the get-go
o Plenty of syntactic sugar like list comprehensions exists
o Splitting the code base into modules helps not getting lost
o OOP with classes is mandatory for this course
CONCLUSION
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
WORKSHOPDON‘T HESITATE
TO ASK QUESTIONS!
THANK YOU FOR YOUR ATTENTION!
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
1. Write a program that prints:„Whats up, python here.“
2. Write a program which divdes two variables.Define the variables a and b. Save the result into a third variable (c) and display it.
3. Write a program that calculates the Fibonacci sequence. Calculate the 𝑛𝑡ℎ (𝑛-te = 🦆) Fibonacci number, given by a user input. (n = 3; F(3) = 2).Display the result.
WORKSHOP
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
4. Design the following scenario in OOP-based code:A bike seller has an inventory of different bikes that he can sell. Each bike has a price and is made of either aluminum, steel or carbon. Furthermore, a bike has a color, a number of gears, and a tire size. Additionally to the normal bikes, the seller offers e-bikes that have a motor with a certain amount of power in watts.When the seller sells a bike, it is removed from the inventory. The bike seller can also buy a bike that is added to the inventory.
WORKSHOP
robolab2020python intro
Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden
COMPARING VARIABLES: is vs. ==
Listing 4: compare.py