Introduction to Python and Regular Expressions in Python Lecture 4.

Slides:



Advertisements
Similar presentations
Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
CS Lecture 03 Outline Sed and awk from previous lecture Writing simple bash script Assignment 1 discussion 1CS 311 Operating SystemsLecture 03.
CS 497C – Introduction to UNIX Lecture 33: - Shell Programming Chin-Chih Chang
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Geography 465 Assignments, Conditionals, and Loops.
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Python Control of Flow.
Python.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
High-Level Programming Languages: C++
The if statement and files. The if statement Do a code block only when something is True if test: print "The expression is true"
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSCI/CMPE 4341 Topic: Programming in Python Review: Exam I Xiang Lian The University of Texas – Pan American Edinburg, TX 78539
9/14/2015BCHB Edwards Introduction to Python BCHB Lecture 4.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
(A Very Short) Introduction to Shell Scripts CSCI N321 – System and Network Administration Copyright © 2000, 2003 by Scott Orr and the Trustees of Indiana.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Xuan Guo Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
CompSci 6 Introduction to Computer Science November 8, 2011 Prof. Rodger.
Decision Structures, String Comparison, Nested Structures
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 6 – sed, command-line tools wrapup.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
The If Statement There are no switch statements in Python. You need to use just if statements. There are no switch statements in Python. You need to use.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
Chapter 5 The Bourne Shell Graham Glass and King Ables, UNIX for Programmers and Users, Third Edition, Pearson Prentice Hall, Notes by Michael Weeks.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
 Type Called bool  Bool has only two possible values: True and False.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Introduction to Python
CSE 374 Programming Concepts & Tools
Introduction to Python
Computer Programming Fundamentals
Perl Programming Language Design and Implementation (4th Edition)
Python: Control Structures
Shell Scripting March 1st, 2004 Class Meeting 7.
Introduction to Python
An Introduction to Python
Introduction to Python
Introduction to Python
Chapter 5 The Bourne Shell
Introduction to Computer Science
Introduction to Bash Programming, part 3
Lecture 31 – Practice Exercises 7
Presentation transcript:

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 –x = = yx is equal to y –x != yx is not equal to y –x > yx is greater than y –x < yx is less than y –x >= yx is greater than or equal to y –x <= yx 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 1.Find all numbers that include years, percentages, dollar amounts and other numeric figures in one regular expression. 2.Find all coated statements (i.e “statement”) 3.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) 4.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) 5.What strings are matched by the following regular expressions? (a) [a-zA-Z]+ (b) [A-Z][a-z]+ (c) \w+|[^\w\s]