Python in 30 Minutes! : Fariz Darari
Python in 30 Minutes! : Fariz Darari
*
Fariz Darari
2
But this Python!
3
But this Python!
Programming Language
4
But this Python!
Programming Language
5
But this Python!
Cross Platform
Programming Language
6
But this Python!
Cross Platform
Programming Language
Created in 1991 by Guido van Rossum Freely Usable Even for Commercial Use
7
print("Python" + " is " + "cool!")
8
print("Python" + " is " + "cool!")
9
print("Python" + " is " + "cool!")
10
print("Python" + " is " + "cool!")
11
print("Python" + " is " + "cool!")
12
print("Python" + " is " + "cool!")
https://opencv.org/
Image Processing using Python
14
print("Python" + " is " + "cool!")
https://www.pygame.org
Game Development using Python
15
print("Python" + " is " + "cool!")
https://matplotlib.org/
Data Science using Python 16
print("Python" + " is " + "cool!")
https://github.com/amueller/word_cloud
Natural Language Processing (NLP) and Text Mining using Python
17
Let's now explore the Python universe!
18
Python Setup
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
Variables and Data Types
• 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 "="
x = 1 # this is a Python comment
x = x + 5
y = "Python" + " is " + "cool!"
20
Variables and Data Types in Real Life
21
Conditionals and Loops
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: 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
24
Conditionals: Usia SIM (Driving license age)
age = 20
if age < 17:
print("Belum bisa punya SIM!")
else:
print("OK, sudah bisa punya SIM.")
25
Conditionals: Usia SIM dengan Input
26
Conditionals: Grading
27
Loops
while boolean-expression:
code-block
30
Conditionals and Loops in Real Life
31
Functions
def celsius_to_fahrenheit(celsius):
fahrenheit = celsius * 1.8 + 32.0
return fahrenheit
33
Functions: Default and Named Parameters
>>> hello()
Hello, Bro & Sis!
>>> hello(name_woman="Lady")
Hello, Bro & Lady!
>>> hello(name_woman="Mbakyu",name_man="Mas")
Hello, Mas & Mbakyu!
34
Imports
• 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))
35
Functions in Real Life
36
String
• String is a sequence of characters, like "Python is cool"
• Each character has an index
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
• 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 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.split(" ")
P y t h o n i s c o o l
0 1 2 3 4 5 0 1 0 1 2 3
",".join(y)
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"
>>> y = x.split(" ")
>>> ",".join(y) 40
Strings in Real Life
41
Input/Output
42
Input
python
is
cool
cool.txt
x.close()
43
Input
python
is
cool
cool.txt
x = open("cool.txt", "r")
x.close() 44
Input
python
is
cool
cool.txt
for line in x:
line = line.replace("\n","")
print(line)
x.close()
45
Output
x.write("carpe\ndiem\n") x.write("carpe\ndiem\n")
x.close() x.close()
47
Output in Real Life
48
Lists
49
List Operations
50
List Comprehension
Example:
[i*2 for i in [0,1,2,3,4] if i%2 == 0]
52
List in Real Life
53
Sets
• 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:
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
54
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'}
55
Dictionary in Real Life
56
Classes
class Person:
def describe(self):
return self.firstname + " " + self.lastname
58
class class-name(base):
class Employee(Person):
def describe(self):
return self.lastname + ", " + str(self.staffnum)
60
Lessons learned
61
What's next?
62
What's next? Keep learning!
https://docs.python.org
Python Official Documentation http://greenteapress.com/wp/think-python-2e/
Free Python e-book
https://stackoverflow.com/questions/tagged/python
Python on stackoverflow (QA website on programming)
63
Food pack is ready,
enjoy your journey!
64