1 / 18

Introduction to Python and Regular Expressions in Python

Introduction to Python and Regular Expressions in Python. Lecture 4. Introduction to Python. Conditions if condition : statement Example: password = raw_input(“Enter your password”) if name == “xyz”: print “XYZ is an authorized user”. Introduction to Python. Conditional Tests

tracy
Download Presentation

Introduction to Python and Regular Expressions in Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to PythonandRegular Expressions in Python Lecture 4

  2. Introduction to Python • Conditions if condition: statement Example: password = raw_input(“Enter your password”) if name == “xyz”: print “XYZ is an authorized user”

  3. Introduction to Python • Conditional Tests • x = = y x is equal to y • x != y x is not equal to y • x > y x is greater than y • x < y x is less than y • x >= y x is greater than or equal to y • x <= y x is less than or equal to y

  4. Introduction to Python • For more than two conditions if condition: statement elif condition: statement else: statement

  5. Introduction to Python Example: password = raw_input(“Enter your password”) if password == “xyz”: print “XYZ is an authorized user” elif password == “abc”: print “ABC has limited access to the system” else: print “Sorry! You do not have an access to the system”

  6. Introduction to Python • Iterations (loop) • Same concept as different popular programming languages (C/C++, Java) only remember : after loop condition while condition: statements

  7. Introduction to Python Example: legal_user = "false" while legal_user == "false": password = raw_input("Enter your password") if password == "xyz": print "XYZ is an authorized user" legal_user = "true" elif password == "abc": print "ABC has limited access to the system" legal_user = "true" else: password = raw_input("Sorry! You do not have an access to the system") legal_user = "false“

  8. Introduction to Python • File Handling • Open (filename, mode) • Mode must be in single quotes r = read w = write (replace existing data) a = append (add to the end) • file_handler.close () • file_handler.read () Read whole file as a string • file_handler.readline () Read a single line as a string • file_handler.readlines () Read whole file, each line becomes string item in a list

  9. Introduction to Python • file_handler.writes (string) This function will write string in the file. • file_handler.writelines (list) This function will write all string items in a list to the file. All string items will be on the same line unless there will be a newline character.

  10. Introduction to Python Example: Copying the contents of one file into other. original_file = open("original.txt", 'r') original_fileList = original_file.readlines() copied_file = open("copied.txt", 'w') for each_line in original_fileList: copied_file.write(each_line) original_file.close() copied_file.close()

  11. Introduction to Python Example: Copying the contents of one file into other. original_file = open("original.txt", 'r') original_fileString = original_file.read() copied_file = open("copied.txt", 'w') copied_file.write(original_fileString) original_file.close() copied_file.close()

  12. Introduction to Python • Defining a function def name (argument): set of commands • def is a keyword • name you have to mention • arguments = initialize variables for function • : = Required at the end of line • set of command must be indented

  13. Introduction to Python Example: # defining the function def print_profile(name, age, address): statement = name + “ is “ + str(age) + “ years of age and he lives in ” + address print statement # use of function print_profile(“XYZ”, 23, “Lahore”)

  14. Introduction to Python • Pre made functions • Use import to include the functionality Example: import math math.pow(4,2)

  15. Regular Expressions in Python Find the following patterns in wsj_0012. • Year (e.g., 1989) import re fileIn = open("wsj_0012.txt", 'r') wholeFile = fileIn.read() year_regular_expression = re.compile('\d\d\d\d') print year_regular_expression.findall(wholeFile) fileIn.close()

  16. Regular Expressions in Python • Dollar Amount ($100,980) import re fileIn = open("wsj_0012.txt", 'r') wholeFile = fileIn.read() year_regular_expression = re.compile('\$\S*[\w\.]') print year_regular_expression.findall(wholeFile) fileIn.close()

  17. Regular Expressions in Python • Percentages (e.g. 7.5%) import re fileIn = open("wsj_0012.txt", 'r') wholeFile = fileIn.read() year_regular_expression = re.compile('\S+%') print year_regular_expression.findall(wholeFile) fileIn.close()

  18. Regular Expressions in Python Assignment • Find all numbers that include years, percentages, dollar amounts and other numeric figures in one regular expression. • Find all coated statements (i.e “statement”) • Count the number of paragraphs and number of words through regular expressions. Each word is only a set of alphabets (hint: see len(list)for counting) • Replace person’s name (i.e. Alan Spoon) with your name. You are not allowed to use “Alan Spoon” as a string to match. (hint: see re.sub() functionality to substitute string) e.g: RE_space = re.compile(‘\s’) sentence_without_space = RE_space.sub(‘’, sentence_with_space) • What strings are matched by the following regular expressions? (a) [a-zA-Z]+ (b) [A-Z][a-z]+ (c) \w+|[^\w\s]

More Related