Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why Python? Introduction Mohamed Yehia Dahab

Similar presentations


Presentation on theme: "Why Python? Introduction Mohamed Yehia Dahab"— Presentation transcript:

1 Why Python? Introduction Mohamed Yehia Dahab

2 Programming Domains Mobile Applications Games Scientific applications
How to represent a very small number (3.1 * )Fortran Business applications Produce reports, use decimal numbers and GUI COBOL Artificial intelligence Symbols rather than numbers manipulated and using logic  Prolog Systems programming Need low level of commands  C Web Applications Scripting (e.g., PHP) Mobile Applications Limited capability of storage and processors. Emulator is needed  Java Games Graphics library is needed  Unity

3 Language Evaluation Criteria
Readability: the ease with which programs can be read and understood Writability: the ease with which a language can be used to create programs Reliability: conformance to specifications (i.e., performs to its specifications) Cost: the ultimate total cost (Speed is included) Portability: The ease with which programs can be moved from one implementation to another Generality: The applicability to a wide range of applications

4 Further Reading

5 Why Python? (Advantages)
Readable and Writable Python takes much less time than Java and C to develop Python programs are typically 3-5 times shorter than equivalent Java programs Supportive Community Support both desktop and web applications Built-in libraries for math and string manipulation Open source libraries for NLP Games Parallel computing ….

6 Why Not Python? (Disadvantages)
Python programs are generally expected to run slower than Java programs Not reliable because of using dynamic binding Hard to debug

7 Suggested IDE’s NetBeans eClipse Eric xyPython https://ideone.com/

8 Topics Variables Data Types Collections Control Statements Functions
Classes Files NLTK (Natural Language Toolkit)

9 Variables A variable is basically a name that represents some value
Python is dynamically typed, no pre-declaration of a variable or its type is necessary # You have to assign a value to a variable before you use it. x = 3 print (x * 2) W = 3.2 print (W * 2) W = ‘3.2'

10 Data Types Boolean Complex Numbers x = True print (x) y = 1>5
print (y) print (y ==0) import cmath print cmath.sqrt(-1) print (1+3j) * (9+4j) a= j print (a.real ) print (a.imag)

11 Data Types (Cont’) String S = 'this is a test'
S = S[:6] + ‘***‘ + S[6:] print (S) S = 'different string' String uid = "sa" pwd = "secret" print (pwd + " is not a good password for " + uid ) print ("%s is not a good password for %s" % (pwd, uid) ) word1 = "A" word2 = "few" word3 = "good" word4 = "words" wordList = ["A", "few", "more", "good", "words"] print ("Words:" + word1 + word2 + word3 + word4) print ("List: " + ' '.join(wordList))

12 Data Types (Cont’) String print ('1+2+3+4+5'.split('+') )
print ('/usr/bin/env'.split('/') ) print ('Using the default'.split()) aString = 'abcd' final_index = len(aString) - 1

13 Regular Expressions Metacharacter Description Examples character
Any literal letter, number, or punctuation character (other than those that follow) matches itself. apple matches apple. (pattern) Patterns can be grouped together using parentheses so that they can be treated as a unit. see following . Match a single character (except linefeed). s.t matches sat, sit, sQt, s3t, s&t, s t,... ? Match zero or one of the previous character/expression. (When immediately following ?, +, *, or {min,max} it prevents the expression from using "greedy" evaluation.) colou?r matches color, colour + Match one or more of the previous character/expression. a+rgh! matches argh!, aargh!, aaargh!,... * Match zero or more of the previous character/expression. b(an)*a matches ba, bana, banana, bananana,... {number} Match exactly number copies of the previous character/expression. .o{2}n matches noon, moon, loon,...

14 Regular Expressions (Cont’)
Metacharacter Description Examples {min,max} Match between min and max copies (inclusive) of the previous character/expression. kabo{2,4}m matches kaboom, kabooom, kaboooom. [set] Match a single character in set (list and/or range). Most characters that have special meanings in regular expressions do not have to be backslash-escaped in character sets. J[aio]b matches Jab, Jib, Job [A-Z][0-9]{3} matches Canadian postal codes. [^set] Match a single character not in set (list and/or range). Most characters that have special meanings in regular expressions do not have to be backslash-escaped in character sets. q[^u] matches very few English words (Iraqi? qoph? qintar?). | Match either expression that it separates. (Mi|U)nix matches Minix and Unix ^ Match the start of a line. ^# matches lines that begin with #. $ Match the end of a line. ^$ matches an empty line.

15 Regular Expressions (Cont’)
Metacharacter Description Examples \ Interpret the next metacharacter character literally, or introduce a special escaped combination (see following). \* matches a literal asterisk. \n Match a single newline (carriage return in Python) character. Hello\nWorld matches Hello World. \t Match a single tab character. Hello\tWorld matches Hello     World. \s Match a single whitespace character. Hello\s+World matches Hello World, Hello  World, Hello   World,... \S Match a single non-whitespace character. \S\S\S matches AAA, The, 5-9,... \d Match a single digit character. \d\d\d matches 123, 409, 982,... \D Match a single non-digit character. \D\D matches It, as, &!,...

16 Regular Expressions in Python
Before you can use regular expressions in your program, you must import the library using "import re“ import re line = 'This from someone' if re.search('from', line) : print ('Found')

17 Regular Expressions in Python (Cont’)
The re.search() returns a True/False depending on whether the string matches the regular expression import re line = '30/5/1999' if re.search('\d+', line) : print ('Found')

18 Regular Expressions in Python (Cont’)
If we want the matching strings, we use re.findall() ['30', '5', '1999'] import re line = '30/5/1999' y = re.findall('\d+', line) print (y)

19 Regular Expressions in Python (Cont’)
Finding proper names, starting with a capital letter ['Ahmed', 'Ali'] import re line = 'I saw Ahmed and Ali' y = re.findall('[A-Z][a-z]+', line) print (y)

20 Regular Expressions in Python (Cont’)
If there is no string matched with regular expression, findall return an empty list [] import re line = 'I saw Ahmed and Ali' y = re.findall('[A-Z][0-9]+', line) print (y)

21 Regular Expressions in Python (Cont’)
Greedy matching is to match the largest possible string ['300$ while Ali found 350$'] import re line = 'Ahmed found 300$ while Ali found 350$' y = re.findall('[0-9].+\$', line) print (y)

22 Regular Expressions in Python (Cont’)
Non-Greedy matching ['300$', '350$'] import re line = 'Ahmed found 300$ while Ali found 350$' y = re.findall('[0-9].+?\$', line) print (y)

23 Regular Expressions in Python (Cont’)
Non-Greedy matching for HTML ['<H1>Some text </H1><H1>Some text again </H1>'] import re line = '<H1>Some text </H1><H1>Some text again </H1>' y = re.findall(‘<H1>.*</H1>', line) print (y)

24 Regular Expressions in Python (Cont’)
Parenthesis are not part of the match - but they tell where to start and stop ['Some text ', 'Some text again '] import re line = '<H1>Some text </H1><H1>Some text again </H1>' y = re.findall(‘<H1>(.*?)</H1>', line) print (y)

25 Regular Expressions in Python (Cont’)
Extracting a host name You can see this code on ['gmail.com'] import re x = 'From Fri Jan 5 09:14: ' y = x) print (y)

26 Collections List Dictionary

27 List Items of a list are separated by commas and enclosed in square brackets numList = [2000, 2003, 2005, 2006] stringList = ["Essential", "Python", "Code"] mixedList = [1, 2, "three", 4] subList = ["Python", "Phrasebook", ["Copyright", 2006]] listList = [numList, stringList, mixedList, subList]

28 List (Cont’) List is direct access like arrays and has dynamic length
numList = [2000, 2003, 2005, 2006] numList .append(2009) numList .insert(2, 2004) print(numList ) print(numList[0] )


Download ppt "Why Python? Introduction Mohamed Yehia Dahab"

Similar presentations


Ads by Google