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 Lan angua uage Programming Language Created in 1991 by Guido van Rossum
Python Cro ross Platform Pro rogramming Lan angua uage Programming Language Created in 1991 by Guido van Rossum Freely Usable Even for Commercial Use
6 Lessons from Dropbox o One Million Files Saved Every 15 minutes (2011) o Use Pyt ython o 99.9 % of their code is in Python o server backend o desktop client o website controller logic o API backend o analytics o Can't use Python on the Android due to memory constraints o Runs on a single code base using Python. Dropbox runs on Win indows, Mac, Lin inux using tools like PyObjs, WxPython, types, py2exe, py2app, PyWin32.
6 Lessons from Dropbox o Pros o Developers talk to each other and express ideas in Python o Easy to learn, easy to read, easy to write, easy for new people to pick up. o Cons o OK, it can use too much memory and be too slow. o Coding in a mixed environment of Python and C creates problems o Memory fragmentation issues are reason why scripting languages may not be a good idea for long running processes
6 Lessons from Dropbox o Just W Work Baby o Shouldn't matter what file system you are on, what OS you are using, what applications you are using. The product should always just work. o Python helped them iterate fast through all the different error cases they experienced on the wide variety of platforms they support.
6 Lessons from Dropbox o Release Early o Code something in a day and release it. Python makes that easy.
6 Lessons from Dropbox o Use C f for In Inner Loops - Optimizing CPU is easy o Poll - Polling 30 30 Milion Clients A All O Over the World Doesn't 't Scale o Custom Memory 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 { print("Hello world!") 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
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 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
Python Setup o https://www.python.org/downloads/
Python Setup o Pyt ython(x,y ,y) ) - https://pyt ython-xy.github.i .io/
Python Command Line o Using command line o Python script in file o comment o Environment variables (Windows) o modules o import sys o sys.argv o dir(sys) o help(sys) o help(sys.exit)
Variables and Data Types
Variables and Data Types o Variables store and give names to data values o Data values can be of various types: o int -5, 0, 1000000 o float -2.0, 3.14159 o bool True, False o str "Hello world!", "K3WL" o list [1, 2, 3, 4], ["Hello", "world!"], [1, 2, "Hello"], [ ] o … o In Python, variables do not have types!
Conditionals and Loops 27
Conditionals and Loops
Conditionals and Loops o Cores of programming! o Rely on boolean expressions which return either True or False o 1 < 2 : True o 1.5 >= 2.5 : Fals lse o answer == "Computer Science" : can be True or Fals lse depending on the value of variable answer o Boolean expressions can be combined with: and and, or or, not o 1 < 2 and 3 < 4 : True o 1.5 >= 2.5 or 2 == 2 : True o not 1.5 >= 2.5 : True
Conditionals and Loops 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
Conditionals and Loops grade = int(raw_input("Numeric grade: ")) if grade >= 80: print("A") elif grade >= 65: print("B") elif grade >= 55: print("C") else: print("E")
Loops o Useful for repeating code! o Two variants:
While Loops
For Loops o So far, we have seen (briefly) two kinds of collections: string and list o For loops can be used to visit e each collection's 's element:
Functions 35
Functions • Functions encapsulate code blocks • Why functions? Modularization and reuse! • Y ou actually have seen examples of functions: • print() • raw_input() • Generic form: def function-name(parameters): code-block return value 36
Functions: Celcius to Fahrenheit def function-name(parameters): code-block return value def celsius_to_fahrenheit(celsius): fahrenheit = celsius * 1.8 + 32.0 return fahrenheit
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! 38
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)) • Generic form: from module_name import function_name from math import sqrt print(sqrt(4))
Strings 40
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])
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" >>> "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 >>> “ Hi %s ” % name # parameters in a string 42
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 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 >>> x = "Python is cool" >>> x.split(" ") 43
String Operations: Join 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
Input/Output 45
Input/Output • Working with data heavily involves reading and writing! • Data come in two types: • T ext: Human readable, encoded in ASCII/UTF-8, example: .txt, .csv • Binary: Machine readable, application-specific encoding, example: .mp3, .mp4, .jpg 46
Recommend
More recommend