28
robolab2020 python intro Robolab 2020 – Sinthujan Thanabalasingam, Christian Braun TU Dresden A SMALL INTRODUCTION TO PYTHON

A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

A SMALL INTRODUCTION TO PYTHON

Page 2: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 3: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

PART 1

BASICS

LET‘S START WITH…

Page 4: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 5: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

INSTALLATION

Page 6: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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?

Page 7: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 8: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

THE FIRST PROGRAM – HELLO WORLD!

Listing 1: hello_world.py

Page 9: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 10: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

THE PYTHON INTERPRETER

https://docs.python.org/3/tutorial/interpreter.html

Page 11: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

WORKSHOPDON‘T HESITATE

TO ASK QUESTIONS!

Page 12: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

PyCharm by IntelliJ

Visual Studio Code

https://www.jetbrains.com/pycharm/

https://code.visualstudio.com

Page 13: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

PART 2

ADVANCED CONCEPTS

LET‘S MOVE ON TO…

Page 14: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

SYNTAX

DETAILS ABOUT THE…

Page 15: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 16: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

VARIABLES & COMMENTS - EXAMPLE

Listing 2: variables.py

Page 17: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

FUNCTIONS & CONDITIONAL FLOW

Listing 3: beers.py

Page 18: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

BLOCKS – INDENTATIONS DETERMINE INSTRUCTION SCOPE

Listing 4: beers.py

Page 19: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

OBJECT-ORIENTEDPROGRAMMING

WHAT IS…?

Page 20: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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)

Page 21: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 22: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

OBJECT-ORIENTED PROGRAMMING (OOP)

Page 23: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

DEMONSTRATIONOF OOP

A LITTLE…

Page 24: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 25: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

WORKSHOPDON‘T HESITATE

TO ASK QUESTIONS!

THANK YOU FOR YOUR ATTENTION!

Page 26: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 27: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

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

Page 28: A SMALL INTRODUCTION TO PYTHON...python intro TU Dresden Robolab 2020 –Sinthujan Thanabalasingam, Christian Braun o Interpreted, dynamically typed language o Widely used in web development,

robolab2020python intro

Robolab 2020 – Sinthujan Thanabalasingam, Christian BraunTU Dresden

COMPARING VARIABLES: is vs. ==

Listing 4: compare.py