SlideShare a Scribd company logo
Introduction to the basics of
Python programming
(PART 3)
by Pedro Rodrigues (pedro@startacareerwithpython.com)
A little about me
{
“Name”: “Pedro Rodrigues”,
“Origin”: {“Country”: “Angola”, “City”: “Luanda”},
“Lives”: [“Netherlands”, 2013],
“Past”: [“CTO”, “Senior Backend Engineer”],
“Present”: [“Freelance Software Engineer”, “Coach”],
“Other”: [“Book author”, “Start a Career with Python”]
}
Why this Meetup Group?
 Promote the usage of Python
 Gather people from different industries and backgrounds
 Teach and Learn
What will be covered
 Recap of Parts 1 and 2
 import, Modules and Packages
 Python in action
 Note: get the code here:
https://dl.dropboxusercontent.com/u/10346356/session3.zip
A little recap
 Python is an interpreted language (CPython is the reference interpreter)
 Variables are names bound to objects stored in memory
 Data Types: immutable or mutable
 Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes,
bytearray), set, dict
 Control Flow: if statement, for loop, while loop
 Iterables are container objects capable of returning their elements one at a time
 Iterators implement the methods __iter__ and __next__
A little recap
 Python is an interpreted language (CPython is the reference interpreter)
 Variables are names bound to objects stored in memory
 Data Types: immutable or mutable
 Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes,
bytearray), set, dict
 Control Flow: if statement, for loop, while loop
 Iterables are container objects capable of returning their elements one at a time
 Iterators implement the methods __iter__ and __next__
A little recap
 Python is an interpreted language (CPython is the reference interpreter)
 Variables are names bound to objects stored in memory
 Data Types: immutable or mutable
 Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes,
bytearray), set, dict
 Control Flow: if statement, for loop, while loop
 Iterables are container objects capable of returning their elements one at a time
 Iterators implement the methods __iter__ and __next__
A little recap
>>> 2 + 2
4
>>> 4 / 2
2.0
>>> 4 > 2
True
>>> x = 1, 2
>>> x
(1, 2)
A little recap
>>> x = [1, 2]
>>> x
[1, 2]
>>> x = {1, 2}
>>> x
{1, 2}
>>> x = {"one": 1, "two": 2}
>>> x
{'two': 2, 'one': 1}
A little recap
 Python is an interpreted language (CPython is the reference interpreter)
 Variables are names bound to objects stored in memory
 Data Types: immutable or mutable
 Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes,
bytearray), set, dict
 Control Flow: if statement, for loop, while loop
 Iterables are container objects capable of returning their elements one at a time
 Iterators implement the methods __iter__ and __next__
A little recap
if x % 3 == 0 and x % 5 == 0:
return "FizzBuzz"
elif x % 3 == 0:
return "Fizz"
elif x % 5 == 0:
return "Buzz"
else:
return x
A little recap
colors = ["red", "green", "blue", "yellow", "purple"]
for color in colors:
if len(color) > 4:
print(color)
stack = [1, 2, 3]
while len(stack) > 0:
print(stack.pop())
A little recap
 Python is an interpreted language (CPython is the reference interpreter)
 Variables are names bound to objects stored in memory
 Data Types: immutable or mutable
 Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes,
bytearray), set, dict
 Control Flow: if statement, for loop, while loop
 Iterables are container objects capable of returning their elements one at a time
 Iterators implement the methods __iter__ and __next__
A little recap
>>> colors = ["red", "green", "blue", "yellow", "purple"]
>>> colors_iter = colors.__iter__()
>>> colors_iter
<list_iterator object at 0x100c7a160>
>>> colors_iter.__next__()
'red'
…
>>> colors_iter.__next__()
'purple'
>>> colors_iter.__next__()
Traceback (most recent call last): File "<stdin>", line 1, in
<module>
StopIteration
A little recap
colors = [(0, "red"), (1, "blue"), (2, "green"), (3, "yellow")]
for index, color in colors:
print(index, " --> ", color)
colors = ["red", "blue", "green", "yellow", "purple"]
for index, color in enumerate(colors):
print(index, " --> ", color)
A little recap
 List comprehensions
 Dictionary comprehensions
 Functions
 Positional Arguments
 Keyword Arguments
 Default parameters
 Variable number of arguments
A little recap
colors = ["red", "green", "blue", "yellow", "purple"]
new_colors = []
for color in colors:
if len(color) > 4:
new_colors.append(color)
new_colors = [color for color in colors if len(color) > 4]
Challenge
 Given a list of colors, create a new list with all the colors in uppercase. Use list
comprehensions.
colors = ["red", "green", "blue", "yellow", "purple"]
upper_colors = []
for color in colors:
upper_colors.append(color.upper())
A little recap
 List comprehensions
 Dictionary comprehensions
 Functions
 Positional Arguments
 Keyword Arguments
 Default parameters
 Variable number of arguments
A little recap
squares = {}
for i in range(10):
squares[i] = i**2
squares = {i:i**2 for i in range(10)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9:
81}
Challenge
 Given a list of colors, create a dictionary where each key is a color and the value is
the color written backwards. Use dict comprehensions.
colors = ["red", "green", "blue", "yellow", "purple"]
backwards_colors = {}
for color in colors:
backwards_colors[color] = color[::-1]
A little recap
 List comprehensions
 Dictionary comprehensions
 Functions
 Positional Arguments
 Keyword Arguments
 Default parameters
 Variable number of arguments
A little recap
def say_hello():
print("Hello")
def add_squares(a, b):
return a**2 + b**2
>>> add_squares(2, 3)
13
def add_squares(a, b=3):
return a**2 + b**2
>>> add_squares(2)
13
A little recap
def add_squares(a, b):
return a**2 + b**2
>>> add_squares(b=3, a=2)
13
A little recap
def add_aquares(*args):
if len(args) == 2:
return args[0]**2 + args[1]**2
>>> add_squares(2, 3)
13
A little recap
def add_squares(**kwargs):
if len(kwargs) == 2:
return kwargs["a"]**2 + kwargs["b"]**2
>>> add_squares(a=2, b=3)
13
Challenge
 Define a function that turns a string into a list of int (operands) and strings (operators) and returns
the list.
>>> _convert_expression("4 3 +")
[4, 3, "+"]
>>> _convert_expression("4 3 + 2 *")
[4, 3, "+", 2, "*"]
 Hints:
 “a b”.split(“ “) = [“a”, “b”]
 “a”.isnumeric() = False
 int(“2”) = 2
 Kudos for who solves in one line using lambdas and list comprehensions.
Challenge
 RPN = Reverse Polish Notation
 4 3 + (7)
 4 3 + 2 * (14)
 Extend RPN calculator to support the operators *, / and sqrt (from math module).
import math
print(math.sqrt(4))
Modules and Packages
 A module is a file with definitions and statements
 It’s named after the file.
 Modules are imported with import statement
 import <module>
 from <module> import <name1>
 from <module> import <name1>, <name2>
 import <module> as <new_module_name>
 from <module> import <name1> as <new_name1>
 from <module> import *
Modules and Packages
 A package is a directory with a special file __init__.py (the file can be empty, and
it’s also not mandatory to exist)
 The file __init__.py is executed when importing a package.
 Packages can contain other packages.
Modules and Packages
api/
__init__.py
rest.py
server.py
services/
__init__.py
rpn.py
hello.py
$ python -m api.server
Modules and Packages
 import api
 from api import rest
 import api.services.rpn
 from api.services.hello import say_hello
Challenge
 Extend functionalities of the RESTful API:
 Add a handler for http://localhost:8080/calculate
 This handler should accept only the POST method.
 Put all the pieces together in the rpn module.
Reading material
 List comprehensions: https://docs.python.org/3.5/tutorial/datastructures.html#list-
comprehensions
 Dict comprehensions:
https://docs.python.org/3.5/tutorial/datastructures.html#dictionaries
 Functions and parameters:
https://docs.python.org/3.5/reference/compound_stmts.html#function-definitions
 Names, Namespaces and Scopes:
https://docs.python.org/3.5/tutorial/classes.html#a-word-about-names-and-
objects
More resources
 Python Tutorial: https://docs.python.org/3/tutorial/index.html
 Python Language Reference: https://docs.python.org/3/reference/index.html
 Slack channel: https://startcareerpython.slack.com/
 Start a Career with Python newsletter: https://www.startacareerwithpython.com/
 Book: Start a Career with Python
 Book 15% off (NZ6SZFBL): https://www.createspace.com/6506874

More Related Content

What's hot

Python
PythonPython
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
Wai Nwe Tun
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
Tiji Thomas
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
Rohan Gupta
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
Panimalar Engineering College
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
Sujith Kumar
 
Python
PythonPython
Python
대갑 김
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
Maulik Borsaniya
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
Luigi De Russis
 
python.ppt
python.pptpython.ppt
python.ppt
shreyas_test_1234
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
Rasan Samarasinghe
 
Python : Data Types
Python : Data TypesPython : Data Types
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
Sidharth Nadhan
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
Kálmán "KAMI" Szalai
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
Amira ElSharkawy
 
Python
PythonPython
Python
Kumar Gaurav
 
Python basics
Python basicsPython basics
Python basics
Hoang Nguyen
 

What's hot (19)

Python
PythonPython
Python
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
 
Matlab and Python: Basic Operations
Matlab and Python: Basic OperationsMatlab and Python: Basic Operations
Matlab and Python: Basic Operations
 
Programming in Python
Programming in Python Programming in Python
Programming in Python
 
Python-The programming Language
Python-The programming LanguagePython-The programming Language
Python-The programming Language
 
Python for Beginners(v1)
Python for Beginners(v1)Python for Beginners(v1)
Python for Beginners(v1)
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
Python
PythonPython
Python
 
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...PYTHON -Chapter 2 - Functions,   Exception, Modules  and    Files -MAULIK BOR...
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
python.ppt
python.pptpython.ppt
python.ppt
 
Python basic
Python basicPython basic
Python basic
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
 
Learn python in 20 minutes
Learn python in 20 minutesLearn python in 20 minutes
Learn python in 20 minutes
 
An introduction to Python for absolute beginners
An introduction to Python for absolute beginnersAn introduction to Python for absolute beginners
An introduction to Python for absolute beginners
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Python
PythonPython
Python
 
Python basics
Python basicsPython basics
Python basics
 

Viewers also liked

Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
ee0703
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
Vijay Chaitanya
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3
Youhei Sakurai
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
Tahani Al-Manie
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
Tim Penhey
 
Python Intro
Python IntroPython Intro
Python Intro
Tim Penhey
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
Haitham El-Ghareeb
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
Laxman Puri
 
Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
Haitham El-Ghareeb
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
amiable_indian
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
Nowell Strite
 

Viewers also liked (12)

Python教程 / Python tutorial
Python教程 / Python tutorialPython教程 / Python tutorial
Python教程 / Python tutorial
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Introduction to python 3
Introduction to python 3Introduction to python 3
Introduction to python 3
 
Python 3 Programming Language
Python 3 Programming LanguagePython 3 Programming Language
Python 3 Programming Language
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Python Intro
Python IntroPython Intro
Python Intro
 
Python Tutorial Part 1
Python Tutorial Part 1Python Tutorial Part 1
Python Tutorial Part 1
 
Python Programming Language
Python Programming LanguagePython Programming Language
Python Programming Language
 
Python Tutorial Part 2
Python Tutorial Part 2Python Tutorial Part 2
Python Tutorial Part 2
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)Python 101: Python for Absolute Beginners (PyTexas 2014)
Python 101: Python for Absolute Beginners (PyTexas 2014)
 
Introduction to Python
Introduction to PythonIntroduction to Python
Introduction to Python
 

Similar to Introduction to the basics of Python programming (part 3)

Python Training
Python TrainingPython Training
Python Training
TIB Academy
 
01-Python-Basics.ppt
01-Python-Basics.ppt01-Python-Basics.ppt
01-Python-Basics.ppt
VicVic56
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
Fadlie Ahdon
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
Simplilearn
 
Biopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and OutlookBiopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and Outlook
Asociación Argentina de Bioinformática y Biología Computacional
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
Ali Huseyn Aliyev
 
Python_IoT.pptx
Python_IoT.pptxPython_IoT.pptx
Python_IoT.pptx
SwatiChoudhary95
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
DRVaibhavmeshram1
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
Leonardo Jimenez
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshers
rajkamaltibacademy
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
HarishParthasarathy4
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptx
SovannDoeur
 
Advance python
Advance pythonAdvance python
Advance python
pulkit agrawal
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
CP-Union
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
Sasidhar Kothuru
 
python-2021.pdf
python-2021.pdfpython-2021.pdf
python-2021.pdf
IsaacKingDiran1
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
Prerna Sharma
 

Similar to Introduction to the basics of Python programming (part 3) (20)

Python Training
Python TrainingPython Training
Python Training
 
01-Python-Basics.ppt
01-Python-Basics.ppt01-Python-Basics.ppt
01-Python-Basics.ppt
 
CPPDS Slide.pdf
CPPDS Slide.pdfCPPDS Slide.pdf
CPPDS Slide.pdf
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Biopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and OutlookBiopython: Overview, State of the Art and Outlook
Biopython: Overview, State of the Art and Outlook
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
Python_IoT.pptx
Python_IoT.pptxPython_IoT.pptx
Python_IoT.pptx
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
 
Python Training Tutorial for Frreshers
Python Training Tutorial for FrreshersPython Training Tutorial for Frreshers
Python Training Tutorial for Frreshers
 
Python 3.pptx
Python 3.pptxPython 3.pptx
Python 3.pptx
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptx
 
Advance python
Advance pythonAdvance python
Advance python
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
Spsl iv unit final
Spsl iv unit  finalSpsl iv unit  final
Spsl iv unit final
 
python-2021.pdf
python-2021.pdfpython-2021.pdf
python-2021.pdf
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
INTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptxINTRODUCTION TO PYTHON.pptx
INTRODUCTION TO PYTHON.pptx
 
Programming in C sesion 2
Programming in C sesion 2Programming in C sesion 2
Programming in C sesion 2
 

Recently uploaded

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 

Recently uploaded (20)

LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 

Introduction to the basics of Python programming (part 3)

  • 1. Introduction to the basics of Python programming (PART 3) by Pedro Rodrigues (pedro@startacareerwithpython.com)
  • 2. A little about me { “Name”: “Pedro Rodrigues”, “Origin”: {“Country”: “Angola”, “City”: “Luanda”}, “Lives”: [“Netherlands”, 2013], “Past”: [“CTO”, “Senior Backend Engineer”], “Present”: [“Freelance Software Engineer”, “Coach”], “Other”: [“Book author”, “Start a Career with Python”] }
  • 3. Why this Meetup Group?  Promote the usage of Python  Gather people from different industries and backgrounds  Teach and Learn
  • 4. What will be covered  Recap of Parts 1 and 2  import, Modules and Packages  Python in action  Note: get the code here: https://dl.dropboxusercontent.com/u/10346356/session3.zip
  • 5. A little recap  Python is an interpreted language (CPython is the reference interpreter)  Variables are names bound to objects stored in memory  Data Types: immutable or mutable  Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes, bytearray), set, dict  Control Flow: if statement, for loop, while loop  Iterables are container objects capable of returning their elements one at a time  Iterators implement the methods __iter__ and __next__
  • 6. A little recap  Python is an interpreted language (CPython is the reference interpreter)  Variables are names bound to objects stored in memory  Data Types: immutable or mutable  Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes, bytearray), set, dict  Control Flow: if statement, for loop, while loop  Iterables are container objects capable of returning their elements one at a time  Iterators implement the methods __iter__ and __next__
  • 7. A little recap  Python is an interpreted language (CPython is the reference interpreter)  Variables are names bound to objects stored in memory  Data Types: immutable or mutable  Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes, bytearray), set, dict  Control Flow: if statement, for loop, while loop  Iterables are container objects capable of returning their elements one at a time  Iterators implement the methods __iter__ and __next__
  • 8. A little recap >>> 2 + 2 4 >>> 4 / 2 2.0 >>> 4 > 2 True >>> x = 1, 2 >>> x (1, 2)
  • 9. A little recap >>> x = [1, 2] >>> x [1, 2] >>> x = {1, 2} >>> x {1, 2} >>> x = {"one": 1, "two": 2} >>> x {'two': 2, 'one': 1}
  • 10. A little recap  Python is an interpreted language (CPython is the reference interpreter)  Variables are names bound to objects stored in memory  Data Types: immutable or mutable  Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes, bytearray), set, dict  Control Flow: if statement, for loop, while loop  Iterables are container objects capable of returning their elements one at a time  Iterators implement the methods __iter__ and __next__
  • 11. A little recap if x % 3 == 0 and x % 5 == 0: return "FizzBuzz" elif x % 3 == 0: return "Fizz" elif x % 5 == 0: return "Buzz" else: return x
  • 12. A little recap colors = ["red", "green", "blue", "yellow", "purple"] for color in colors: if len(color) > 4: print(color) stack = [1, 2, 3] while len(stack) > 0: print(stack.pop())
  • 13. A little recap  Python is an interpreted language (CPython is the reference interpreter)  Variables are names bound to objects stored in memory  Data Types: immutable or mutable  Data Types: Numbers (int, float, bool), Sequences (str, tuple, list, bytes, bytearray), set, dict  Control Flow: if statement, for loop, while loop  Iterables are container objects capable of returning their elements one at a time  Iterators implement the methods __iter__ and __next__
  • 14. A little recap >>> colors = ["red", "green", "blue", "yellow", "purple"] >>> colors_iter = colors.__iter__() >>> colors_iter <list_iterator object at 0x100c7a160> >>> colors_iter.__next__() 'red' … >>> colors_iter.__next__() 'purple' >>> colors_iter.__next__() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration
  • 15. A little recap colors = [(0, "red"), (1, "blue"), (2, "green"), (3, "yellow")] for index, color in colors: print(index, " --> ", color) colors = ["red", "blue", "green", "yellow", "purple"] for index, color in enumerate(colors): print(index, " --> ", color)
  • 16. A little recap  List comprehensions  Dictionary comprehensions  Functions  Positional Arguments  Keyword Arguments  Default parameters  Variable number of arguments
  • 17. A little recap colors = ["red", "green", "blue", "yellow", "purple"] new_colors = [] for color in colors: if len(color) > 4: new_colors.append(color) new_colors = [color for color in colors if len(color) > 4]
  • 18. Challenge  Given a list of colors, create a new list with all the colors in uppercase. Use list comprehensions. colors = ["red", "green", "blue", "yellow", "purple"] upper_colors = [] for color in colors: upper_colors.append(color.upper())
  • 19. A little recap  List comprehensions  Dictionary comprehensions  Functions  Positional Arguments  Keyword Arguments  Default parameters  Variable number of arguments
  • 20. A little recap squares = {} for i in range(10): squares[i] = i**2 squares = {i:i**2 for i in range(10)} {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}
  • 21. Challenge  Given a list of colors, create a dictionary where each key is a color and the value is the color written backwards. Use dict comprehensions. colors = ["red", "green", "blue", "yellow", "purple"] backwards_colors = {} for color in colors: backwards_colors[color] = color[::-1]
  • 22. A little recap  List comprehensions  Dictionary comprehensions  Functions  Positional Arguments  Keyword Arguments  Default parameters  Variable number of arguments
  • 23. A little recap def say_hello(): print("Hello") def add_squares(a, b): return a**2 + b**2 >>> add_squares(2, 3) 13 def add_squares(a, b=3): return a**2 + b**2 >>> add_squares(2) 13
  • 24. A little recap def add_squares(a, b): return a**2 + b**2 >>> add_squares(b=3, a=2) 13
  • 25. A little recap def add_aquares(*args): if len(args) == 2: return args[0]**2 + args[1]**2 >>> add_squares(2, 3) 13
  • 26. A little recap def add_squares(**kwargs): if len(kwargs) == 2: return kwargs["a"]**2 + kwargs["b"]**2 >>> add_squares(a=2, b=3) 13
  • 27. Challenge  Define a function that turns a string into a list of int (operands) and strings (operators) and returns the list. >>> _convert_expression("4 3 +") [4, 3, "+"] >>> _convert_expression("4 3 + 2 *") [4, 3, "+", 2, "*"]  Hints:  “a b”.split(“ “) = [“a”, “b”]  “a”.isnumeric() = False  int(“2”) = 2  Kudos for who solves in one line using lambdas and list comprehensions.
  • 28. Challenge  RPN = Reverse Polish Notation  4 3 + (7)  4 3 + 2 * (14)  Extend RPN calculator to support the operators *, / and sqrt (from math module). import math print(math.sqrt(4))
  • 29. Modules and Packages  A module is a file with definitions and statements  It’s named after the file.  Modules are imported with import statement  import <module>  from <module> import <name1>  from <module> import <name1>, <name2>  import <module> as <new_module_name>  from <module> import <name1> as <new_name1>  from <module> import *
  • 30. Modules and Packages  A package is a directory with a special file __init__.py (the file can be empty, and it’s also not mandatory to exist)  The file __init__.py is executed when importing a package.  Packages can contain other packages.
  • 32. Modules and Packages  import api  from api import rest  import api.services.rpn  from api.services.hello import say_hello
  • 33. Challenge  Extend functionalities of the RESTful API:  Add a handler for http://localhost:8080/calculate  This handler should accept only the POST method.  Put all the pieces together in the rpn module.
  • 34. Reading material  List comprehensions: https://docs.python.org/3.5/tutorial/datastructures.html#list- comprehensions  Dict comprehensions: https://docs.python.org/3.5/tutorial/datastructures.html#dictionaries  Functions and parameters: https://docs.python.org/3.5/reference/compound_stmts.html#function-definitions  Names, Namespaces and Scopes: https://docs.python.org/3.5/tutorial/classes.html#a-word-about-names-and- objects
  • 35. More resources  Python Tutorial: https://docs.python.org/3/tutorial/index.html  Python Language Reference: https://docs.python.org/3/reference/index.html  Slack channel: https://startcareerpython.slack.com/  Start a Career with Python newsletter: https://www.startacareerwithpython.com/  Book: Start a Career with Python  Book 15% off (NZ6SZFBL): https://www.createspace.com/6506874

Editor's Notes

  1. Similar to list comprehensions