String operations; More on function definitions; Conditional execution

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Recitation 1 Programming for Engineers in Python.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
CS61A Lecture 2 Functions and Applicative Model of Computation Tom Magrino and Jon Kotker UC Berkeley EECS June 19, 2012.
Announcements Class / Recitation slides available on the syllabus page Check morning of for latest version We will start grading clickers next lecture.
Programming for Linguists An Introduction to Python 17/11/2011.
Python Programming Fundamentals
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Munster Programming Training
1 CSC 201: Computer Programming I B. S. Afolabi. Introduction  3 unit course  2 hours of lecture/week Thursdays 4.00pm – 6.00pm Mondays 4.00pm – 6.00pm.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Functions Chapter 4 Python for Informatics: Exploring Information
Looping and Counting Lecture 3 Hartmut Kaiser
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
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
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
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.
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
Variables, Types, Expressions Intro2CS – week 1b 1.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Programming Languages Programming languages are a compromise between spoken language and formal math. They allow humans to communicate with computers at.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
ENGINEERING 1D04 Tutorial 1. WELCOME! What we’re doing today Who am I How to do well What are computer programs Software Development Cycle Pseudo Code.
Introduction to Python
Topic: Conditional Statements – Part 2
CS 1428 Exam I Review.
Selection Statements by Ahmet Sacan
Variables, Expressions, and IO
Intro to C Tutorial 4: Arithmetic and Logical expressions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Imperative Programming
And now for something completely different . . .
An Introduction to Python
Functions, Part 1 of 3 Topics Using Predefined Functions
CS 100: Roadmap to Computing
Introduction to Programming Using Python PART 2
Variables, Data Types & Math
Chapter 2 Programming Basics.
Functions, Part 1 of 3 Topics Using Predefined Functions
Python Basics with Jupyter Notebook
Arithmetic Operations
Nate Brunelle Today: Conditional Decision Statements
COMPUTER PROGRAMMING SKILLS
Programming Dr. Jalal Kawash.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
L L Line CSE 420 Computer Games Lecture #3 Introduction to Python.
CS 100: Roadmap to Computing
CS 1428 Exam I Review.
More Basics of Python Common types of data we will work with
Functions, Procedures, and Abstraction
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Imperative Programming
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
COMPUTING.
Introduction to Python
Presentation transcript:

String operations; More on function definitions; Conditional execution Intro2CS – week 2

Essence of Programming Take basic operations Combine them to do something more complex print(“some string”) print(“*******”) print(“* HI! *”)

Abstraction It is often useful to take something complex and encapsulate it. Then we can use it as a new basic operation def print_boxed_hi(): print(“*******”) print(“* HI! *”) print(“And now, a greeting:”) print_boxed_hi()

Basic operations seen so far Output Assignment, Arithmetic operations Turtle stuff print(“Hello World”) r1 = (-b + math.sqrt(b*b – 4*a*c)) / (2*a) turtle.forward(150)

Combination methods seen so far Sequential execution Conditional Execution Repeated Execution (loops) do_something1() do_something2() Combination methods to come if condition: do_something() for x in some_range_of_values: do_something(x)

This Lecture More basic operations on Strings More flexibility in defining new “operations” (functions) Conditional execution Later in the week: loops

What are all the basic operations? “Old school” programming: minimal set of basic operations; come with the language; need to know all of them “New school” programming: many basic operations; can get more libraries; need to know how to find what you need Python philosophy: “batteries included”

Why spend time on String Operations? Example of quickly learning new basic operations No need to remember everything Useful Maybe you should remember… Example of a complex data type More to come later (e.g. list=[3, 5, 8] )

The Authoritative Reference

The Python Standard Library

Strings in The Python Standard Library 1. Introduction 2. Built-in Functions 3. Built-in Constants 3.1. Constants added by the site module 4. Built-in Types 4.1. Truth Value Testing 4.2. Boolean Operations — and, or, not ... 4.7. Text Sequence Type – str … 4.13. … 37. …

Google it

Stack Overflow

Strings are sequences of characters

Negative indices; Slices

Operations

Unicode

Unicode

Special character encoding

Input

More operations

Defining functions Parameters Return Values Abstraction and Encapsulation

Defining and Using

Comments

Parameters def print_twice(message): “““print the given message twice“““ print(message) s = “Hello” print_twice(s)

Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s

Parameters def print_twice(message): “““print the given message twice“““ print(message) Hello s = “Hello” print_twice(s) s message

Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) s = “Hello” print_twice(s)

Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello s = “Hello” print_twice(s) s message

Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello Hello\nHello s = “Hello” print_twice(s) s message

Parameters def print_twice(message): “““print the given message twice“““ message = message + “\n” + message print(message) Hello Hello\nHello s = “Hello” print_twice(s) s message

Scope

Name conflict

Name conflict

Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(s, 3)

Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(message=s, k=3)

Multiple parameters def print_k_times(message, k): “““print k times the given message“““ message = k *(message + “\n”) print(message) s = “Hello” print_k_times(k=3, message=s)

Default values

Returning a value def cubic_root(x): “”” returns the cubic root of x “”” return x**(1/3) y = cubic_root(8) # 2.0 print(cubic_root(y / 2) + 1)

Returning a value import math def fourth_root(x): “”” returns the fourth root of x “”” y = math.sqrt(x) return math.sqrt(y)

Assigning multiple values

Returning multiple values def my_divmod(x, y): “”” returns the quotient and remainder of the integer division x/y“”” return x//y, x%y q, r = my_divmod(23, 4)

Returning multiple values def split_date(s): “”” split a date give in the format dd/mm/yyyy into its components. returns year, date, month (as integers) “”” y = int(s[-4:]) m = int(s[3:5]) d = int(s[:2]) return y, m, d

Conditional Execution If statement More on Boolean values

Combining Operations Do this and then do that do_this() do_that() In case 1 do this; otherwise do that Case 1? yes no do_this() do_this() do_that() do_that() do_this() do_that() if case1(): do_this() else do_that()

If - else

Multiple statements in If-else

Elif and Nested Ifs if age > 18: if country == “Germany”: drink = “beer” elif country == “France”: drink = “wine” elif country == “Russia”: drink = “vodka” else drink = “water” elif age > 5: drink = “soda” else: drink = “milk”

Boolealn Expressions

Boolean Functions

Another Implementation

If as an expression