First Tool: Python!
Introduction to python programming Gholamhossein Tavasoli @ ZNU
First Tool: Python! Introduction to python programming - - PowerPoint PPT Presentation
First Tool: Python! Introduction to python programming Gholamhossein Tavasoli @ ZNU First Tool: Python! Python Programming Language Python Programming Language Created in 1991 by Guido van Rossum Python Cro ross Platform Pro rogramming
Introduction to python programming Gholamhossein Tavasoli @ ZNU
Programming Language
Programming Language Created in 1991 by Guido van Rossum
Programming Language Created in 1991 by Guido van Rossum Cro ross Platform Pro rogramming Lan angua uage
Programming Language Created in 1991 by Guido van Rossum Cro ross Platform Pro rogramming Lan angua uage Freely Usable Even for Commercial Use
ython
indows, Mac, Lin inux using tools like PyObjs, WxPython, types, py2exe, py2app, PyWin32.
people to pick up.
problems
languages may not be a good idea for long running processes
Work Baby
OS you are using, what applications you are using. The product should always just work.
different error cases they experienced on the wide variety of platforms they support.
makes that easy.
for In Inner Loops - Optimizing CPU is easy
30 Milion Clients A All O Over the World Doesn't 't Scale
ry Allocator - Optimizing Memory ry is Hard
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
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
public class Main { public static void main(String[] args) { System.out.println("Hello world!"); } } print("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
Big names using Python
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
https://opencv .org/ Image Processing using Python
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
https://www.pygame.org Game Development using Python
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
https://matplotlib.org/ Data Science using Python
print("Python" + " is " + "cool!")
"Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike." – codeschool.com
https://github.com/amueller/word_cloud Natural Language Processing (NLP) and T ext Mining using Python
ython(x,y ,y) ) - https://pyt ython-xy.github.i .io/
True, False
"Hello world!", "K3WL"
[1, 2, 3, 4], ["Hello", "world!"], [1, 2, "Hello"], [ ]
Conditionals and Loops
27
lse
lse depending on the value of variable answer
and, or
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
grade = int(raw_input("Numeric grade: ")) if grade >= 80: print("A") elif grade >= 65: print("B") elif grade >= 55: print("C") else: print("E")
string and list
each collection's 's element:
Functions
35
def function-name(parameters): code-block return value
Functions
36
def celsius_to_fahrenheit(celsius): fahrenheit = celsius * 1.8 + 32.0 return fahrenheit
def function-name(parameters): code-block return value
38
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!
import math print(math.sqrt(4))
from math import sqrt print(sqrt(4))
Strings
40
x = "Python is cool" print(x[10])
print(x[2:6])
P y t h
i s c
1 2 3 4 5 6 7 8 9 10 11 12 13
42
>>> x = "Python is cool" >>> "cool" in x >>> len(x) >>> x + "?" >>> x.upper() # membership # length of string x # concatenation # to upper case >>> x.replace("c", "k") # replace characters in a string
String Operations
P y t h
i s c
1 2 3 4 5 6 7 8 9 10 11 12 13 >>> “Hi %s” % name # parameters in a string
>>> x = "Python is cool"
String Operations: Split
x.split(" ")
>>> x.split(" ")
43
P y t h
i s c
1 2 3 4 5 6 7 8 9 10 11 12 13 P y t h
1 2 3 4 5 i s 1 c
1 2 3
>>> x = "Python is cool" >>> y = x.split(" ")
String Operations: Join
",".join(y)
>>> ",".join(y)
40
P y t h
, i s , c
1 2 3 4 5 6 7 8 9 10 11 12 13 P y t h
1 2 3 4 5 i s 1 c
1 2 3
Input/Output
45
ext: Human readable, encoded in ASCII/UTF-8, example: .txt, .csv
example: .mp3, .mp4, .jpg
Input/Output
46
python is cool
Input
cool.txt
x = open("cool.txt", "r") # read mode y = x.read() # read the whole print(y) x.close()
47
python is cool
cool.txt
x = open("cool.txt", "r")
44
Input
line = line.replace("\n","") print(line) x.close() # read line by line for line in x:
python is cool
Input
cool.txt
x = open("C:\\Users\\Fariz\\cool.txt", "r") # absolute location for line in x: line = line.replace("\n","") print(line) x.close()
49
Output
# write mode x = open("carpe-diem.txt", "w") x.write("carpe\ndiem\n") x.close() # append mode x = open("carpe-diem.txt", "a")
50
x.write("carpe\ndiem\n") x.close()
Write mode overwrites files, while append mode does not overwrite files but instead appends at the end of the files' content
List
51
52
a list is a sequence of items!
we are free to modify lists (that is, lists are mutable).
x = [1, 2, 3, 4] x[0] = 4 x.append(5) print(x) # [4, 2, 3, 4, 5]
Lists
50
List Operations
>>> x = [ 1, "Python", "is", "cool" ] >>> x.sort() >>> x[0:2] >>> len(x) >>> x + ["!"] >>> x[2] = "hot" # sort elements in x # slicing # length of string x # concatenation # 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 >>> [1, 2, 3] + [4, 5] Mutable vs None-Mutable
54
It is basically a cool way of generating a list
[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]
List Comprehension
55
except functions modifying the tuples' content
x = (0,1,2) y = 0,1,2 # same as x x[0] = 2 # this gives an error
Tuples
56
there is no order of elements!
Sets
Dictionary
57
58
Dictionaries
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'}
Class
59
in classes we encapsulate objects!
class class-name(base): attribute-code-block method-code-block
Classes
60
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())
Classes: Person
class class-name(base): attribute-code-block method-code-block
61
59
Classes: Person & Employee
# first add code for class Person here
class class-name(base): attribute-code-block method-code-block
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())