180 likes | 426 Views
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
E N D
Introduction to PythonandRegular 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 • 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
Introduction to Python • For more than two conditions if condition: statement elif condition: statement else: statement
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”
Introduction to Python • Iterations (loop) • Same concept as different popular programming languages (C/C++, Java) only remember : after loop condition while condition: statements
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“
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
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.
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()
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()
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
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”)
Introduction to Python • Pre made functions • Use import to include the functionality Example: import math math.pow(4,2)
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()
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()
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()
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]