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.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Python Basics: Statements Expressions Loops Strings Functions.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Introduction to Computing Using Python Imperative Programming  Python Programs  Interactive Input/Output  One-Way and Two-Way if Statements  for Loops.
Programming in python Lesson 2.
ECS15 for and sequence. Comments  Another way to repeat.
Structured programming
Strings, if/else, return, user input
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Introduction to Python
Python quick start guide
Week #2 Java Programming. Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check.
Programming for Linguists An Introduction to Python 17/11/2011.
Python Programming Fundamentals
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Introduction to Python
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 6 Functions -- QuickStart. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. What is a function?
07/10/ Strings ASCII& Processing Strings with the Functions - Locate (Instr), Mid, Length (Len), Char (ChrW) & ASCII (Asc)
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Introduction to Computing Using Python Straight-line code  In chapter 2, code was “straight-line”; the Python statements in a file (or entered into the.
Input, Output, and Processing
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
If statements while loop for loop
1. Exam Topics Difference between computers and calculators John creates a new device. It will compute the orbit of all the planets in the solar system.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Last Week if statement print statement input builtin function strings and methods for loop.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Decision Structures, String Comparison, Nested Structures
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
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.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Student Q and As from 5.1 – 5.7, 5.11 Students of CS104, Fall 2013 (and me)
Python Strings. String  A String is a sequence of characters  Access characters one at a time with a bracket operator and an offset index >>> fruit.
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Introduction to Python Developed by Dutch programmer Guido van Rossum Named after Monty Python Open source development project Simple, readable language.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Python Basics.
Introduction to Programming
Introduction to Programming
Python: Experiencing IDLE, writing simple programs
Introduction to Python
Line Continuation, Output Formatting, and Decision Structures
Basic operators - strings
Decision Structures, String Comparison, Nested Structures
Line Continuation, Output Formatting, and Decision Structures
Decision Structures, String Comparison, Nested Structures
Introduction to Programming
Basic String Operations
Python Basics with Jupyter Notebook
Introduction to Computer Science
COMPUTER PROGRAMMING SKILLS
Introduction to Computer Science
Introduction to Programming
Presentation transcript:

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 Docstrings Use to explain the code Must be a string and the first line in a function or module Include parameter names, mention their types and describe the return value and type

This Week More on modules New Python statements if statement print statement details input builtin function for … in …: statement The boolean type

Modules Sometimes we want to use some functions frequently Save them to a file, e.g., filename.py Include them with the Python command import filename We call this file a module.

Modules Python has builtin functions, e.g., –pow(x,y) returns x y –sqrt(x) returns √x Organized into different modules (stored in different files) pow(x,y) and sqrt(x) belong to the math module

More on import import math –Need to say math.sqrt(2,3) to use sqrt() –Prevents ambiguity –But inconvenient to type math.sqrt Solution –Only import specific functions: >>> from math import pow, sqrt –Import all functions when you know there are no conflicts >>> from math import * –Now we can say sqrt(5)

The __builtins__ Module Ways to get help with functions: –dir(module): list the functions in a module –help(function): show the docstrings for a function or module –import module: include the functions defined in a module

The “if” Statement English example: Check The Temperature: If the temperature > 0 then it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

The “if” Statement Python example: def check_temp(temperature): If the temperature > 0 then it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” elif temperature == 0: return “at the freezing point” Otherwise it is “below the freezing point”

The “if” Statement Python example: def check_temp(temperature): if temperature > 0: return “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

The “if” Statement EnglishPython If conditionif condition: Otherwise, if conditionelif condition: Otherwiseelse:

Nested “if” Statements English example: Check The Temperature: If the temperature > 0 then if the temperature >100 then it is “above the boiling point” Otherwise, if the temperature> 37 then it is “above body temperature” Otherwise, it is “above the freezing point” Otherwise, if the temperature = 0 then it is “at the freezing point” Otherwise it is “below the freezing point”

Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if the temperature >100 then it is “above the boiling point” Otherwise, if the temperature> 37 then it is “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” Otherwise, if the temperature > 37 then it is “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

Nested “if” Statements English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” elif temperature > 37: return “above body temperature” Otherwise, it is “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point”

English example: def check_temp(temperature): if temperature > 0: if temperature > 100: return “above the boiling point” elif temperature > 37: return “above body temperature” else: return “above the freezing point” elif temperature == 0: return “at the freezing point” else: return “below the freezing point” Nested “if” Statements >100 >37 and <=100 >0 and <=37

Getting User Input When we want to display information to the screen we use print. >>> print(“hello”) >>> print(5+6) When we want get input from the user we use the builtin function input. >>> input() >>> name = input(“Enter name:”)

Getting User Input Q. What does input return? A. input() always returns a string. Suppose we need an integer value from the user. >>> age = input(“Enter your age:”) Q. How can we convert input’s return value to be an integer? A. >>> age = int(input(“Enter your age:”))

More on Strings Q. What is a string exactly? A. A sequence of characters with position numbers called indices. “Hello World”: “H” is at index 0. Q. How can we select parts of a string? A. We can select a particular character by specifying its index: >>>”Hello World”[2] “l”“l”

More on Strings We can select part of a string by specifying a range. >>>”Hello Class”[1:6] “ello ” Q. What string would “Hello Class”[3:] return? A. >>>”Hello Class”[3:] “lo Class” Q. What string would “Hello Class”[:2] return? A. >>>”Hello Class”[:2] “He”

More on Strings Q. What else can we do to strings? A. There are many methods that apply to strings: “Hello World”.replace(“World”,“Class”) “Hello Class”.count(“l”) “Hello Class”.find(“as”) The functions replace, count and find are called methods because they behave like operators. Q. How would you find all the methods for strings? A. dir(str)

String Methods s.isupper(): returns True if s is all upper case, False otherwise. s.islower(): returns True if s is all lower case, False otherwise. s.isdigit(): returns True if s is a number, False otherwise. s.upper(): returns a copy of s in all upper case. s.lower(): returns a copy of s in all lower case.

String Methods len(s): returns the length of s. sub in s: returns true if sub is a substring of s. We know what “anna” + “anna” returns. Q. What about “anna”*4 ? “annaannaannaanna”

Visiting the Items in a String S Printing out the characters of the string English: for each char in S print the char Python: for char in S: print(char) Notes: char is a variable name for and in are Python key words

for loops Format: for variable in string: statements Example with strings: name = ”Edward” new = “” for letter in name: new = letter + new print(new)

Strings Using Conversion Specifiers We sometimes would like to insert values of variables into strings: A1 = 60 A2 = 75 A3 = 88 We would like: ‘The average of 60, 75 and 88 is ’ How do we print this with our variables? >>>print(‘The average of’, A1, ‘,’, A2, ‘ and ’, A3, ‘ is ’, (A1+A2+A3)/3) Does this work?

Strings Using Conversion Specifiers We displayed: ‘The average of 60, 75 and 88 is ’ Q. What’s wrong? A. Spacing is wrong around commas and periods. We have many more decimal places than wanted. Q. How can we fix it? A. Use conversion specifiers. >>>print(‘The average of %d, %d and %d is %.2f’ %(A1, A2, A3, (A1+A2+A3)/3.0)) The average of 60, 75 and 88 is

Common Conversion Specifiers %d display the object as a decimal integer %fdisplay the object as a floating point with 6 decimal places %.2fdisplay the object as a floating point with 2 decimal places %sdisplay the object as a string Q. What else do we use % for? A. Modulus. We say that % is overloaded.