Why Python? Introduction Mohamed Yehia Dahab

Slides:



Advertisements
Similar presentations
ISBN Chapter 6 Data Types Character Strings Pattern Matching.
Advertisements

LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
ISBN Chapter 1 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter 1 Topics Motivation Programming Domains.
CS 354 Overview. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Course Topics What is a programming language? What features do programming.
ISBN Lecture 01 Preliminaries. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Lecture 01 Topics Motivation Programming.
Chapter 2 Data Types, Declarations, and Displays
JavaScript, Third Edition
ISBN Chapter 1 Topics Motivation Programming Domains Language Evaluation Criteria Influences on Language Design Language Categories Language.
String Escape Sequences
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Last Updated March 2006 Slide 1 Regular Expressions.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
Regular Expressions Chapter 11 Python for Informatics: Exploring Information
1 CSC 594 Topics in AI – Text Mining and Analytics Fall 2015/16 4. Document Search and Regular Expressions.
ISBN CS 354 Preliminaries. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Course Topics What is a programming language? What features.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CSC 4630 Meeting 7 February 7, 2007.
Finding the needle(s) in the textual haystack
Regular Expressions Upsorn Praphamontripong CS 1110
CS 330 Class 7 Comments on Exam Programming plan for today:
Chapter 1 Preliminaries.
“Under the hood”: Angry Birds Maze
Concepts of Programming Languages
Strings and Serialization
Looking for Patterns - Finding them with Regular Expressions
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
PROGRAMMING LANGUAGES
Concepts of Programming Languages
Regular Expressions Chapter 11 Python for Everybody
Chapter 6: Data Types Lectures # 10.
Structured Programming
Scope, Objects, Strings, Numbers
Finding the needle(s) in the textual haystack
Concepts of Programming Languages
Finding the needle(s) in the textual haystack
Pattern Matching in Strings
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Chapter 1 Introduction.
Introduction to Python
Introduction to C++ Programming
INFO/CSE 100, Spring 2005 Fluency in Information Technology
Chapter 1 Preliminaries.
PHP.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Python Primer 1: Types and Operators
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Output Manipulation.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Chapter 1 Preliminaries.
Introduction to Computer Science
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Why Python? Introduction Mohamed Yehia Dahab https://www.researchgate.net/profile/Mohamed_Dahab

Programming Domains Mobile Applications Games Scientific applications How to represent a very small number (3.1 * 10-270)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

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

Further Reading

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 ….

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

Suggested IDE’s NetBeans eClipse Eric xyPython https://ideone.com/ https://wiki.python.org/moin/IntegratedDevelopmentEnvironments

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

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'

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=1.5+0.5j print (a.real ) print (a.imag)

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))

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

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,...

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.

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, &!,...

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 email from someone' if re.search('from', line) : print ('Found')

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')

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)

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)

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)

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)

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)

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)

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)

Regular Expressions in Python (Cont’) Extracting a host name You can see this code on https://ideone.com/aF3pDE ['gmail.com'] import re x = 'From mohamed.dahab@gmail.com Fri Jan 5 09:14:16 2016' y = re.findall('@(.*?)\s', x) print (y)

Collections List Dictionary

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]

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] )