SlideShare a Scribd company logo
Python in 30 minutes!*
Fariz Darari
* Recommended slide reading time unless otherwise specified
2
Not this python, unfortunately
3
But this Python!
4
But this Python!
Programming Language
5
But this Python!
Programming Language
Created in 1991 by Guido van Rossum
6
But this Python!
Cross Platform
Programming Language
Created in 1991 by Guido van Rossum
7
But this Python!
Programming Language
Freely Usable Even for Commercial UseCreated in 1991 by Guido van Rossum
Cross Platform
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
8
print("Python" + " is " + "cool!")
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
9
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
10
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
11
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
print("Hello world!")
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
12
print("Python" + " is " + "cool!")
13
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
Big names using Python
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
14
print("Python" + " is " + "cool!")
Image Processing using Python
https://opencv.org/
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
15
print("Python" + " is " + "cool!")
Game Development using Python
https://www.pygame.org
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
16
print("Python" + " is " + "cool!")
Data Science using Python
https://matplotlib.org/
"Python is easy to use, powerful, and versatile, making it a great choice
for beginners and experts alike." – codeschool.com
17
print("Python" + " is " + "cool!")
Natural Language Processing (NLP) and Text Mining using Python
https://github.com/amueller/word_cloud
18
Let's now explore the Python universe!
How to install Python the Anaconda way
1. Download Anaconda (which includes Python):
https://www.anaconda.com/download/
2. Run the installer and follow the installation instructions
3. Run the Spyder editor and create your first Python program "helloworld.py"
19
Python Setup
• Variables store and give names to data values
• Data values can be of various types:
• int : -5, 0, 1000000
• float : -2.0, 3.14159
• bool : True, False
• str : "Hello world!", "K3WL"
• list : [1, 2, 3, 4], ["Hello", "world!"], [1, 2, "Hello"], [ ]
• And many more!
• In Python, variables do not have types!
• Data values are assigned to variables using "="
20
Variables and Data Types
x = 1 # this is a Python comment
x = x + 5
y = "Python" + " is " + "cool!"
21
Variables and Data Types in Real Life
22
Conditionals and Loops
• Cores of programming!
• Rely on boolean expressions which return either True or False
• 1 < 2 : True
• 1.5 >= 2.5 : False
• answer == "Computer Science" :
can be True or False depending on the value of variable answer
• Boolean expressions can be combined with: and, or, not
• 1 < 2 and 3 < 4 : True
• 1.5 >= 2.5 or 2 == 2 : True
• not 1.5 >= 2.5 : True
23
Conditionals and Loops
24
Conditionals: Generic Form
if boolean-expression-1:
code-block-1
elif boolean-expression-2:
code-block-2
(as many elif's as you want)
else:
code-block-last
25
Conditionals: Usia SIM (Driving license age)
age = 20
if age < 17:
print("Belum bisa punya SIM!")
else:
print("OK, sudah bisa punya SIM.")
26
Conditionals: Usia SIM dengan Input
age = int(raw_input("Usia: ")) # use input() for Python 3
if age < 17:
print("Belum bisa punya SIM!")
else:
print("OK, sudah bisa punya SIM.")
27
Conditionals: Grading
grade = int(raw_input("Numeric grade: "))
if grade >= 80:
print("A")
elif grade >= 65:
print("B")
elif grade >= 55:
print("C")
else:
print("E")
• Useful for repeating code!
• Two variants:
28
Loops
while boolean-expression:
code-block
for element in collection:
code-block
29
While Loops
while raw_input("Which is the best subject? ") != "Computer Science":
print("Try again!")
print("Of course it is!")
while boolean-expression:
code-block
So far, we have seen (briefly) two kinds of collections:
string and list
For loops can be used to visit each collection's element:
30
For Loops
for element in collection:
code-block
for chr in "string":
print(chr)
for elem in [1,3,5]:
print(elem)
Conditionals and Loops in Real Life
31
• Functions encapsulate (= membungkus) code blocks
• Why functions? Modularization and reuse!
• You actually have seen examples of functions:
• print()
• raw_input()
• Generic form:
32
Functions
def function-name(parameters):
code-block
return value
33
Functions: Celcius to Fahrenheit
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 1.8 + 32.0
return fahrenheit
def function-name(parameters):
code-block
return value
34
Functions: Default and Named Parameters
def hello(name_man="Bro", name_woman="Sis"):
print("Hello, " + name_man + " & " + name_woman + "!")
>>> hello()
Hello, Bro & Sis!
>>> hello(name_woman="Lady")
Hello, Bro & Lady!
>>> hello(name_woman="Mbakyu",name_man="Mas")
Hello, Mas & Mbakyu!
• Code made by other people shall be reused!
• Two ways of importing modules (= Python files):
• Generic form: import module_name
import math
print(math.sqrt(4))
• Generic form: from module_name import function_name
from math import sqrt
print(sqrt(4))
35
Imports
Functions in Real Life
36
• String is a sequence of characters, like "Python is cool"
• Each character has an index
• Accessing a character: string[index]
x = "Python is cool"
print(x[10])
• Accessing a substring via slicing: string[start:finish]
print(x[2:6])
37
String
P y t h o n i s c o o l
0 1 2 3 4 5 6 7 8 9 10 11 12 13
>>> x = "Python is cool"
>>> "cool" in x # membership
>>> len(x) # length of string x
>>> x + "?" # concatenation
>>> x.upper() # to upper case
>>> x.replace("c", "k") # replace characters in a string
38
String Operations
P y t h o n i s c o o l
0 1 2 3 4 5 6 7 8 9 10 11 12 13
>>> x = "Python is cool"
>>> x.split(" ") 39
String Operations: Split
P y t h o n i s c o o l
0 1 2 3 4 5 6 7 8 9 10 11 12 13
P y t h o n
0 1 2 3 4 5
i s
0 1
c o o l
0 1 2 3
x.split(" ")
>>> x = "Python is cool"
>>> y = x.split(" ")
>>> ",".join(y) 40
String Operations: Join
P y t h o n , i s , c o o l
0 1 2 3 4 5 6 7 8 9 10 11 12 13
P y t h o n
0 1 2 3 4 5
i s
0 1
c o o l
0 1 2 3
",".join(y)
Strings in Real Life
41
• Working with data heavily involves reading and writing!
• Data come in two types:
• Text: Human readable, encoded in ASCII/UTF-8, example: .txt, .csv
• Binary: Machine readable, application-specific encoding,
example: .mp3, .mp4, .jpg
42
Input/Output
python
is
cool
43
Input
cool.txt
x = open("cool.txt", "r") # read mode
y = x.read() # read the whole
print(y)
x.close()
python
is
cool
44
Input
cool.txt
x = open("cool.txt", "r")
# read line by line
for line in x:
line = line.replace("n","")
print(line)
x.close()
python
is
cool
45
Input
cool.txt
x = open("C:UsersFarizcool.txt", "r") # absolute location
for line in x:
line = line.replace("n","")
print(line)
x.close()
46
Output
# write mode
x = open("carpe-diem.txt", "w")
x.write("carpendiemn")
x.close()
# append mode
x = open("carpe-diem.txt", "a")
x.write("carpendiemn")
x.close()
Write mode overwrites files,
while append mode does not overwrite files but instead appends at the end of the files' content
Input in Real Life
47
Output in Real Life
48
• If a string is a sequence of characters, then
a list is a sequence of items!
• List is usually enclosed by square brackets [ ]
• As opposed to strings where the object is fixed (= immutable),
we are free to modify lists (that is, lists are mutable).
49
Lists
x = [1, 2, 3, 4]
x[0] = 4
x.append(5)
print(x) # [4, 2, 3, 4, 5]
50
List Operations
>>> x = [ "Python", "is", "cool" ]
>>> x.sort() # sort elements in x
>>> x[0:2] # slicing
>>> len(x) # length of string x
>>> x + ["!"] # concatenation
>>> x[2] = "hot" # replace element at index 0 with "hot"
>>> x.remove("Python") # remove the first occurrence of "Python"
>>> x.pop(0) # remove the element at index 0
It is basically a cool way of generating a list
51
List Comprehension
[expression for-clause condition]
Example:
[i*2 for i in [0,1,2,3,4] if i%2 == 0]
[i.replace("o", "i") for i in ["Python", "is", "cool"] if len(i) >= 3]
• Like a list, but you cannot modify it (= immutable)
• Tuple is usually (but not necessarily) enclosed by parentheses ()
• Everything that works with lists, works with tuples,
except functions modifying the tuples' content
• Example:
52
Tuples
x = (0,1,2)
y = 0,1,2 # same as x
x[0] = 2 # this gives an error
List in Real Life
53
• As opposed to lists, in sets duplicates are removed and
there is no order of elements!
• Set is of the form { e1, e2, e3, ... }
• Operations include: intersection, union, difference.
• Example:
54
Sets
x = [0,1,2,0,0,1,2,2]
y = {0,1,2,0,0,1,2,2}
print(x)
print(y)
print(y & {1,2,3}) # intersection
print(y | {1,2,3}) # union
print(y - {1,2,3}) # difference
55
Dictionaries
• Dictionaries map from keys to values!
• Content in dictionaries is not ordered.
• Dictionary is of the form { k1:v1, k2:v2, k3:v3, ... }
• Example:
x = {"indonesia":"jakarta", "germany":"berlin","italy":"rome"}
print(x["indonesia"]) # get value from key
x["japan"] = "tokyo" # add a new key-value pair to dictionary
print(x) # {'italy': 'rome', 'indonesia': 'jakarta', 'germany': 'berlin', 'japan': 'tokyo'}
Dictionary in Real Life
56
• While in functions we encapsulate a set of instructions,
in classes we encapsulate objects!
• A class is a blueprint for objects, specifying:
• Attributes for objects
• Methods for objects
• A class can use other classes as a base
• Generic:
57
Classes
class class-name(base):
attribute-code-block
method-code-block
class Person:
def __init__(self, first, last):
self.firstname = first
self.lastname = last
def describe(self):
return self.firstname + " " + self.lastname
guido = Person("Guido","Van Rossum")
print(guido.describe())
58
Classes: Person
class class-name(base):
attribute-code-block
method-code-block
59
Classes: Person & Employee
class class-name(base):
attribute-code-block
method-code-block
# first add code for class Person here
class Employee(Person):
def __init__(self, first, last, staffnum):
Person.__init__(self, first, last)
self.staffnum = staffnum
def describe(self):
return self.lastname + ", " + str(self.staffnum)
guido = Employee("Guido", "Van Rossum", 123456)
print(guido.describe())
Class in Real Life
60
61
Lessons learned
62
What's next?
63
What's next? Keep learning!
https://stackoverflow.com/questions/tagged/python
https://docs.python.org
http://greenteapress.com/wp/think-python-2e/Python Official Documentation
Python on stackoverflow (QA website on programming)
Free Python e-book
64
Food pack is ready,
enjoy your journey!

More Related Content

What's hot (20)

PDF
Introduction to python programming
Srinivas Narasegouda
 
PPTX
Python basics
Hoang Nguyen
 
PDF
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Edureka!
 
PPSX
Programming with Python
Rasan Samarasinghe
 
PPT
Intro to Python
primeteacher32
 
PPTX
Python Tutorial Part 1
Haitham El-Ghareeb
 
PPTX
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
PDF
Introduction to python
Learnbay Datascience
 
PDF
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Edureka!
 
PDF
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Edureka!
 
PPTX
Python programming introduction
Siddique Ibrahim
 
PPTX
Python 3 Programming Language
Tahani Al-Manie
 
PPTX
Looping statement in python
RaginiJain21
 
PPT
Python Programming ppt
ismailmrribi
 
PPTX
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
PPTX
Basics of python
SurjeetSinghSurjeetS
 
PPTX
Python - An Introduction
Swarit Wadhe
 
PPT
Introduction to python
Syed Zaid Irshad
 
PPTX
Fundamentals of Python Programming
Kamal Acharya
 
Introduction to python programming
Srinivas Narasegouda
 
Python basics
Hoang Nguyen
 
Python Programming Language | Python Classes | Python Tutorial | Python Train...
Edureka!
 
Programming with Python
Rasan Samarasinghe
 
Intro to Python
primeteacher32
 
Python Tutorial Part 1
Haitham El-Ghareeb
 
Introduction to the basics of Python programming (part 1)
Pedro Rodrigues
 
Introduction to python
Learnbay Datascience
 
Python Course | Python Programming | Python Tutorial | Python Training | Edureka
Edureka!
 
Python Tutorial | Python Tutorial for Beginners | Python Training | Edureka
Edureka!
 
Python programming introduction
Siddique Ibrahim
 
Python 3 Programming Language
Tahani Al-Manie
 
Looping statement in python
RaginiJain21
 
Python Programming ppt
ismailmrribi
 
Python 101: Python for Absolute Beginners (PyTexas 2014)
Paige Bailey
 
Basics of python
SurjeetSinghSurjeetS
 
Python - An Introduction
Swarit Wadhe
 
Introduction to python
Syed Zaid Irshad
 
Fundamentals of Python Programming
Kamal Acharya
 

Similar to Python in 30 minutes! (20)

PPTX
cupdf.com_python-seminar-ppt.pptx.........
ansuljoshi8456
 
PPTX
Introduction to Python Programming Language
merlinjohnsy
 
PPTX
Python Introduction
Punithavel Ramani
 
PPTX
Python Seminar PPT
Shivam Gupta
 
PPTX
Python
Shivam Gupta
 
PDF
python-160403194316.pdf
gmadhu8
 
PPT
Python - Module 1.ppt
jaba kumar
 
PPT
Python tutorialfeb152012
Shani729
 
PDF
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
PDF
Python indroduction
FEG
 
ODP
An Intro to Python in 30 minutes
Sumit Raj
 
PDF
05 python.pdf
SugumarSarDurai
 
PPTX
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
PDF
Python: An introduction A summer workshop
ForrayFerenc
 
PPTX
IOT notes,................................
taetaebts431
 
PPTX
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
PPTX
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
PPTX
python presntation 2.pptx
Arpittripathi45
 
PPTX
Python Tutorial for Beginner
rajkamaltibacademy
 
PPTX
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
cupdf.com_python-seminar-ppt.pptx.........
ansuljoshi8456
 
Introduction to Python Programming Language
merlinjohnsy
 
Python Introduction
Punithavel Ramani
 
Python Seminar PPT
Shivam Gupta
 
Python
Shivam Gupta
 
python-160403194316.pdf
gmadhu8
 
Python - Module 1.ppt
jaba kumar
 
Python tutorialfeb152012
Shani729
 
web programming UNIT VIII python by Bhavsingh Maloth
Bhavsingh Maloth
 
Python indroduction
FEG
 
An Intro to Python in 30 minutes
Sumit Raj
 
05 python.pdf
SugumarSarDurai
 
unit (1)INTRODUCTION TO PYTHON course.pptx
usvirat1805
 
Python: An introduction A summer workshop
ForrayFerenc
 
IOT notes,................................
taetaebts431
 
Chapter 1-Introduction and syntax of python programming.pptx
atharvdeshpande20
 
INTRODUCTION TO PYTHON.pptx
Nimrahafzal1
 
python presntation 2.pptx
Arpittripathi45
 
Python Tutorial for Beginner
rajkamaltibacademy
 
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Ad

More from Fariz Darari (20)

PDF
Data X Museum - Hari Museum Internasional 2022 - WMID
Fariz Darari
 
PDF
[PUBLIC] quiz-01-midterm-solutions.pdf
Fariz Darari
 
PPTX
Free AI Kit - Game Theory
Fariz Darari
 
PPTX
Neural Networks and Deep Learning: An Intro
Fariz Darari
 
PPTX
NLP guest lecture: How to get text to confess what knowledge it has
Fariz Darari
 
PPTX
Supply and Demand - AI Talents
Fariz Darari
 
PPTX
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
PPTX
AI in education done properly
Fariz Darari
 
PPTX
Artificial Neural Networks: Pointers
Fariz Darari
 
PPTX
Open Tridharma at ICACSIS 2019
Fariz Darari
 
PDF
Defense Slides of Avicenna Wisesa - PROWD
Fariz Darari
 
PPTX
Seminar Laporan Aktualisasi - Tridharma Terbuka - Fariz Darari
Fariz Darari
 
PPTX
Foundations of Programming - Java OOP
Fariz Darari
 
PPTX
Recursion in Python
Fariz Darari
 
PDF
[ISWC 2013] Completeness statements about RDF data sources and their use for ...
Fariz Darari
 
PPTX
Testing in Python: doctest and unittest (Updated)
Fariz Darari
 
PPTX
Testing in Python: doctest and unittest
Fariz Darari
 
PPTX
Dissertation Defense - Managing and Consuming Completeness Information for RD...
Fariz Darari
 
PPTX
Research Writing - 2018.07.18
Fariz Darari
 
PPTX
KOI - Knowledge Of Incidents - SemEval 2018
Fariz Darari
 
Data X Museum - Hari Museum Internasional 2022 - WMID
Fariz Darari
 
[PUBLIC] quiz-01-midterm-solutions.pdf
Fariz Darari
 
Free AI Kit - Game Theory
Fariz Darari
 
Neural Networks and Deep Learning: An Intro
Fariz Darari
 
NLP guest lecture: How to get text to confess what knowledge it has
Fariz Darari
 
Supply and Demand - AI Talents
Fariz Darari
 
Basic Python Programming: Part 01 and Part 02
Fariz Darari
 
AI in education done properly
Fariz Darari
 
Artificial Neural Networks: Pointers
Fariz Darari
 
Open Tridharma at ICACSIS 2019
Fariz Darari
 
Defense Slides of Avicenna Wisesa - PROWD
Fariz Darari
 
Seminar Laporan Aktualisasi - Tridharma Terbuka - Fariz Darari
Fariz Darari
 
Foundations of Programming - Java OOP
Fariz Darari
 
Recursion in Python
Fariz Darari
 
[ISWC 2013] Completeness statements about RDF data sources and their use for ...
Fariz Darari
 
Testing in Python: doctest and unittest (Updated)
Fariz Darari
 
Testing in Python: doctest and unittest
Fariz Darari
 
Dissertation Defense - Managing and Consuming Completeness Information for RD...
Fariz Darari
 
Research Writing - 2018.07.18
Fariz Darari
 
KOI - Knowledge Of Incidents - SemEval 2018
Fariz Darari
 
Ad

Recently uploaded (20)

PDF
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PDF
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
PPTX
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
4K Video Downloader Plus Pro Crack for MacOS New Download 2025
bashirkhan333g
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
MiniTool Partition Wizard Free Crack + Full Free Download 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Technical-Careers-Roadmap-in-Software-Market.pdf
Hussein Ali
 
Milwaukee Marketo User Group - Summer Road Trip: Mapping and Personalizing Yo...
bbedford2
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Build It, Buy It, or Already Got It? Make Smarter Martech Decisions
bbedford2
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 

Python in 30 minutes!

  • 1. Python in 30 minutes!* Fariz Darari * Recommended slide reading time unless otherwise specified
  • 2. 2 Not this python, unfortunately
  • 5. 5 But this Python! Programming Language Created in 1991 by Guido van Rossum
  • 6. 6 But this Python! Cross Platform Programming Language Created in 1991 by Guido van Rossum
  • 7. 7 But this Python! Programming Language Freely Usable Even for Commercial UseCreated in 1991 by Guido van Rossum Cross Platform
  • 8. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 8 print("Python" + " is " + "cool!")
  • 9. print("Python" + " is " + "cool!") "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 9
  • 10. print("Python" + " is " + "cool!") "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 10 public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } }
  • 11. print("Python" + " is " + "cool!") "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 11 public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } } print("Hello world!")
  • 12. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 12 print("Python" + " is " + "cool!")
  • 13. 13 print("Python" + " is " + "cool!") "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com Big names using Python
  • 14. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 14 print("Python" + " is " + "cool!") Image Processing using Python https://opencv.org/
  • 15. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 15 print("Python" + " is " + "cool!") Game Development using Python https://www.pygame.org
  • 16. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 16 print("Python" + " is " + "cool!") Data Science using Python https://matplotlib.org/
  • 17. "Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com 17 print("Python" + " is " + "cool!") Natural Language Processing (NLP) and Text Mining using Python https://github.com/amueller/word_cloud
  • 18. 18 Let's now explore the Python universe!
  • 19. How to install Python the Anaconda way 1. Download Anaconda (which includes Python): https://www.anaconda.com/download/ 2. Run the installer and follow the installation instructions 3. Run the Spyder editor and create your first Python program "helloworld.py" 19 Python Setup
  • 20. • Variables store and give names to data values • Data values can be of various types: • int : -5, 0, 1000000 • float : -2.0, 3.14159 • bool : True, False • str : "Hello world!", "K3WL" • list : [1, 2, 3, 4], ["Hello", "world!"], [1, 2, "Hello"], [ ] • And many more! • In Python, variables do not have types! • Data values are assigned to variables using "=" 20 Variables and Data Types x = 1 # this is a Python comment x = x + 5 y = "Python" + " is " + "cool!"
  • 21. 21 Variables and Data Types in Real Life
  • 23. • Cores of programming! • Rely on boolean expressions which return either True or False • 1 < 2 : True • 1.5 >= 2.5 : False • answer == "Computer Science" : can be True or False depending on the value of variable answer • Boolean expressions can be combined with: and, or, not • 1 < 2 and 3 < 4 : True • 1.5 >= 2.5 or 2 == 2 : True • not 1.5 >= 2.5 : True 23 Conditionals and Loops
  • 24. 24 Conditionals: Generic Form if boolean-expression-1: code-block-1 elif boolean-expression-2: code-block-2 (as many elif's as you want) else: code-block-last
  • 25. 25 Conditionals: Usia SIM (Driving license age) age = 20 if age < 17: print("Belum bisa punya SIM!") else: print("OK, sudah bisa punya SIM.")
  • 26. 26 Conditionals: Usia SIM dengan Input age = int(raw_input("Usia: ")) # use input() for Python 3 if age < 17: print("Belum bisa punya SIM!") else: print("OK, sudah bisa punya SIM.")
  • 27. 27 Conditionals: Grading grade = int(raw_input("Numeric grade: ")) if grade >= 80: print("A") elif grade >= 65: print("B") elif grade >= 55: print("C") else: print("E")
  • 28. • Useful for repeating code! • Two variants: 28 Loops while boolean-expression: code-block for element in collection: code-block
  • 29. 29 While Loops while raw_input("Which is the best subject? ") != "Computer Science": print("Try again!") print("Of course it is!") while boolean-expression: code-block
  • 30. So far, we have seen (briefly) two kinds of collections: string and list For loops can be used to visit each collection's element: 30 For Loops for element in collection: code-block for chr in "string": print(chr) for elem in [1,3,5]: print(elem)
  • 31. Conditionals and Loops in Real Life 31
  • 32. • Functions encapsulate (= membungkus) code blocks • Why functions? Modularization and reuse! • You actually have seen examples of functions: • print() • raw_input() • Generic form: 32 Functions def function-name(parameters): code-block return value
  • 33. 33 Functions: Celcius to Fahrenheit def celsius_to_fahrenheit(celsius): fahrenheit = celsius * 1.8 + 32.0 return fahrenheit def function-name(parameters): code-block return value
  • 34. 34 Functions: Default and Named Parameters def hello(name_man="Bro", name_woman="Sis"): print("Hello, " + name_man + " & " + name_woman + "!") >>> hello() Hello, Bro & Sis! >>> hello(name_woman="Lady") Hello, Bro & Lady! >>> hello(name_woman="Mbakyu",name_man="Mas") Hello, Mas & Mbakyu!
  • 35. • Code made by other people shall be reused! • Two ways of importing modules (= Python files): • Generic form: import module_name import math print(math.sqrt(4)) • Generic form: from module_name import function_name from math import sqrt print(sqrt(4)) 35 Imports
  • 36. Functions in Real Life 36
  • 37. • String is a sequence of characters, like "Python is cool" • Each character has an index • Accessing a character: string[index] x = "Python is cool" print(x[10]) • Accessing a substring via slicing: string[start:finish] print(x[2:6]) 37 String P y t h o n i s c o o l 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  • 38. >>> x = "Python is cool" >>> "cool" in x # membership >>> len(x) # length of string x >>> x + "?" # concatenation >>> x.upper() # to upper case >>> x.replace("c", "k") # replace characters in a string 38 String Operations P y t h o n i s c o o l 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  • 39. >>> x = "Python is cool" >>> x.split(" ") 39 String Operations: Split P y t h o n i s c o o l 0 1 2 3 4 5 6 7 8 9 10 11 12 13 P y t h o n 0 1 2 3 4 5 i s 0 1 c o o l 0 1 2 3 x.split(" ")
  • 40. >>> x = "Python is cool" >>> y = x.split(" ") >>> ",".join(y) 40 String Operations: Join P y t h o n , i s , c o o l 0 1 2 3 4 5 6 7 8 9 10 11 12 13 P y t h o n 0 1 2 3 4 5 i s 0 1 c o o l 0 1 2 3 ",".join(y)
  • 41. Strings in Real Life 41
  • 42. • Working with data heavily involves reading and writing! • Data come in two types: • Text: Human readable, encoded in ASCII/UTF-8, example: .txt, .csv • Binary: Machine readable, application-specific encoding, example: .mp3, .mp4, .jpg 42 Input/Output
  • 43. python is cool 43 Input cool.txt x = open("cool.txt", "r") # read mode y = x.read() # read the whole print(y) x.close()
  • 44. python is cool 44 Input cool.txt x = open("cool.txt", "r") # read line by line for line in x: line = line.replace("n","") print(line) x.close()
  • 45. python is cool 45 Input cool.txt x = open("C:UsersFarizcool.txt", "r") # absolute location for line in x: line = line.replace("n","") print(line) x.close()
  • 46. 46 Output # write mode x = open("carpe-diem.txt", "w") x.write("carpendiemn") x.close() # append mode x = open("carpe-diem.txt", "a") x.write("carpendiemn") x.close() Write mode overwrites files, while append mode does not overwrite files but instead appends at the end of the files' content
  • 47. Input in Real Life 47
  • 48. Output in Real Life 48
  • 49. • If a string is a sequence of characters, then a list is a sequence of items! • List is usually enclosed by square brackets [ ] • As opposed to strings where the object is fixed (= immutable), we are free to modify lists (that is, lists are mutable). 49 Lists x = [1, 2, 3, 4] x[0] = 4 x.append(5) print(x) # [4, 2, 3, 4, 5]
  • 50. 50 List Operations >>> x = [ "Python", "is", "cool" ] >>> x.sort() # sort elements in x >>> x[0:2] # slicing >>> len(x) # length of string x >>> x + ["!"] # concatenation >>> x[2] = "hot" # replace element at index 0 with "hot" >>> x.remove("Python") # remove the first occurrence of "Python" >>> x.pop(0) # remove the element at index 0
  • 51. It is basically a cool way of generating a list 51 List Comprehension [expression for-clause condition] Example: [i*2 for i in [0,1,2,3,4] if i%2 == 0] [i.replace("o", "i") for i in ["Python", "is", "cool"] if len(i) >= 3]
  • 52. • Like a list, but you cannot modify it (= immutable) • Tuple is usually (but not necessarily) enclosed by parentheses () • Everything that works with lists, works with tuples, except functions modifying the tuples' content • Example: 52 Tuples x = (0,1,2) y = 0,1,2 # same as x x[0] = 2 # this gives an error
  • 53. List in Real Life 53
  • 54. • As opposed to lists, in sets duplicates are removed and there is no order of elements! • Set is of the form { e1, e2, e3, ... } • Operations include: intersection, union, difference. • Example: 54 Sets x = [0,1,2,0,0,1,2,2] y = {0,1,2,0,0,1,2,2} print(x) print(y) print(y & {1,2,3}) # intersection print(y | {1,2,3}) # union print(y - {1,2,3}) # difference
  • 55. 55 Dictionaries • Dictionaries map from keys to values! • Content in dictionaries is not ordered. • Dictionary is of the form { k1:v1, k2:v2, k3:v3, ... } • Example: x = {"indonesia":"jakarta", "germany":"berlin","italy":"rome"} print(x["indonesia"]) # get value from key x["japan"] = "tokyo" # add a new key-value pair to dictionary print(x) # {'italy': 'rome', 'indonesia': 'jakarta', 'germany': 'berlin', 'japan': 'tokyo'}
  • 57. • While in functions we encapsulate a set of instructions, in classes we encapsulate objects! • A class is a blueprint for objects, specifying: • Attributes for objects • Methods for objects • A class can use other classes as a base • Generic: 57 Classes class class-name(base): attribute-code-block method-code-block
  • 58. class Person: def __init__(self, first, last): self.firstname = first self.lastname = last def describe(self): return self.firstname + " " + self.lastname guido = Person("Guido","Van Rossum") print(guido.describe()) 58 Classes: Person class class-name(base): attribute-code-block method-code-block
  • 59. 59 Classes: Person & Employee class class-name(base): attribute-code-block method-code-block # first add code for class Person here class Employee(Person): def __init__(self, first, last, staffnum): Person.__init__(self, first, last) self.staffnum = staffnum def describe(self): return self.lastname + ", " + str(self.staffnum) guido = Employee("Guido", "Van Rossum", 123456) print(guido.describe())
  • 60. Class in Real Life 60
  • 63. 63 What's next? Keep learning! https://stackoverflow.com/questions/tagged/python https://docs.python.org http://greenteapress.com/wp/think-python-2e/Python Official Documentation Python on stackoverflow (QA website on programming) Free Python e-book
  • 64. 64 Food pack is ready, enjoy your journey!

Editor's Notes

  • #2: http://www.unixstickers.com/stickers/coding_stickers/python-language-coding-badge-sticker https://creativecommons.org/licenses/
  • #3: African rock pythons are the largest species of snake in Africa.   PHOTOGRAPH BY JOEL SARTORE, NATIONAL GEOGRAPHIC PHOTO ARK https://emojiisland.com/pages/download-new-emoji-icons-in-png-ios-10
  • #4: https://gvanrossum.github.io/ https://www.iconfinder.com/icons/532716/api_coding_configuration_development_html_programming_window_icon#size=512 https://www.iconfinder.com/icons/314811/lock_open_icon#size=512 https://jumpcloud.com/daas-product/device-management/
  • #5: https://gvanrossum.github.io/ https://www.iconfinder.com/icons/532716/api_coding_configuration_development_html_programming_window_icon#size=512 https://www.iconfinder.com/icons/314811/lock_open_icon#size=512 https://jumpcloud.com/daas-product/device-management/
  • #6: https://gvanrossum.github.io/ https://www.iconfinder.com/icons/532716/api_coding_configuration_development_html_programming_window_icon#size=512 https://www.iconfinder.com/icons/314811/lock_open_icon#size=512 https://jumpcloud.com/daas-product/device-management/
  • #7: https://gvanrossum.github.io/ https://www.iconfinder.com/icons/532716/api_coding_configuration_development_html_programming_window_icon#size=512 https://www.iconfinder.com/icons/314811/lock_open_icon#size=512 https://jumpcloud.com/daas-product/device-management/
  • #8: https://gvanrossum.github.io/ https://www.iconfinder.com/icons/532716/api_coding_configuration_development_html_programming_window_icon#size=512 https://www.iconfinder.com/icons/314811/lock_open_icon#size=512 https://jumpcloud.com/daas-product/device-management/
  • #9: Why Python? Inspired from: https://www.codeschool.com/blog/2016/01/27/why-python/
  • #13: Powerful wrt influence (big companies are using it) Credits: https://www.theverge.com/2015/9/1/9239769/new-google-logo-announced https://commons.wikimedia.org/wiki/File:Netflix_2015_logo.svg https://www.underconsideration.com/brandnew/archives/new_identity_for_spotify_by_collins.php https://commons.wikimedia.org/wiki/File:Philips_logo_new.svg
  • #14: Powerful wrt influence (big companies are using it) Credits: https://www.theverge.com/2015/9/1/9239769/new-google-logo-announced https://commons.wikimedia.org/wiki/File:Netflix_2015_logo.svg https://www.underconsideration.com/brandnew/archives/new_identity_for_spotify_by_collins.php https://commons.wikimedia.org/wiki/File:Philips_logo_new.svg
  • #15: Credits: https://docs.opencv.org/master/dc/dd3/tutorial_gausian_median_blur_bilateral_filter.html
  • #16: Credits: https://cmlsc.itch.io/lollipop-ninja
  • #17: Credits: https://matplotlib.org/gallery/pyplots/whats_new_99_mplot3d.html#sphx-glr-gallery-pyplots-whats-new-99-mplot3d-py
  • #18: Credits: https://nlpforhackers.io/word-clouds/ http://www.nltk.org/
  • #19: python features utk text mining dan nlp: lists, loops, import, functions, list comprehension, strings slicing indexing, set, i/o Credits: https://docs.microsoft.com/en-us/media/common/i_get-started.svg https://emojiisland.com/products/snake-emoji-icon https://www.pexels.com/photo/sky-space-milky-way-stars-110854/
  • #22: https://www.pexels.com/photo/aroma-aromatic-assortment-bottles-531446/
  • #23: Shapes' meaning: https://support.office.com/en-us/article/create-a-basic-flowchart-e207d975-4a51-4bfa-a356-eeec314bd276 Sumber: https://www.bbc.co.uk/education/guides/z3bq7ty/revision/3
  • #32: https://www.pexels.com/photo/lights-high-colorful-ferris-wheel-40547/
  • #33: Modularization: So that your code can be better managed, unlike one huge program -> small functions, glued into one
  • #37: https://www.pexels.com/photo/amg-gt-automobile-automotive-button-498701/
  • #38: start: where we start taking the substring finish: the index one after we end the substring
  • #42: https://www.pexels.com/photo/alphabet-boogle-dice-enjoy-262529/ This Latin phrase, which literally means "pluck the day," was used by the Roman poet Horace to express the idea that we should enjoy life while we can. His full injunction, "carpe diem quam minimum credula postero,” can be translated as “pluck the day, trusting as little as possible in the next one,” but carpe diem alone has come to be used as shorthand for this entire idea, which is more widely known as "seize the day."
  • #48: https://www.pexels.com/photo/long-blonde-haired-woman-eating-ice-cream-789694/
  • #49: https://www.pexels.com/photo/baby-child-close-up-crying-47090/
  • #52: [0, 4, 8] ['Pythin', 'ciil']
  • #54: https://www.pexels.com/photo/orange-and-green-pen-on-graphing-notepad-131979/
  • #57: https://www.pexels.com/photo/blur-book-close-up-data-270233/
  • #59: https://www.python-course.eu/python3_inheritance.php
  • #60: https://www.python-course.eu/python3_inheritance.php
  • #61: https://commons.wikimedia.org/wiki/File:Blauwdruk-Ronhaar.jpg 1923 blueprint for shophouse with bakery Ronhaar at the Hammerweg in Ommen, demolished in 2007; the almost flat upper part of the mansard roof is found in the central and eastern Netherlands, but is virtually unknown in the river area and in the southern part of the Netherlands.
  • #65: https://www.pexels.com/photo/baked-basket-blur-bread-350350/