CATHERINE AND ANNIE Python: Part 4. Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

RAPTOR Syntax and Semantics By Lt Col Schorsch
Python Programming Language
CS0007: Introduction to Computer Programming
Introduction to Computing Science and Programming I
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
16-May-15 Sudden Python Drinking from the Fire Hose.
Conditional Statements Introduction to Computing Science and Programming I.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Simple Python Loops Sec 9-7 Web Design.
Fundamentals of Python: From First Programs Through Data Structures
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Strings The Basics. Strings can refer to a string variable as one variable or as many different components (characters) string values are delimited by.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Decision Structures, String Comparison, Nested Structures
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Midterm Review Important control structures Functions Loops Conditionals Important things to review Binary Boolean operators (and, or, not) Libraries (import.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Decision Making and Branching
Instructor: Chris Trenkov Hands-on Course Python for Absolute Beginners (Spring 2015) Class #003 (February 14, 2015)
Conditionals.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
ENGINEERING 1D04 Tutorial 2. What we’re doing today More on Strings String input Strings as lists String indexing Slice Concatenation and Repetition len()
Unit 1 Logical operators.
Component 1 Logical operators.
Whatcha doin'? Aims: To start using Python. To understand loops.
Python Let’s get started!.
Tuples and Lists.
Loops BIS1523 – Lecture 10.
Lesson 4 - Challenges.
Python: Control Structures
Debugging and Random Numbers
IF statements.
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Variables, Expressions, and IO
Programming 101 Programming for non-programmers.
Logical Operators and While Loops
And now for something completely different . . .
Conditions and Ifs BIS1523 – Lecture 8.
String Encodings and Penny Math
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Python programming exercise
Python Programming Language
ICT Programming Lesson 3:
Logical Operators and While Loops
CHAPTER 5: Control Flow Tools (if statement)
String Encodings and Penny Math
Presentation transcript:

CATHERINE AND ANNIE Python: Part 4

Strings  Strings are interesting creatures. Although words are strings, anything contained within a set of quotes is treated as a string in Python  Examples: ‘38949’ is a string, as is ‘…\w{][{}’, as is ‘hello world!’  Spaces count as characters! So do punctuation marks  Computers treat strings like ‘lists’ of characters, all strung together  So you can access each character of a string using exactly the same syntax that you would to access an element in a list  In your folder, open the file that says “stringExample.py”  Right-click > Edit with IDLE  How do you think the for loop in this program works? What would happen if we didn’t include the comma after the print statement in the loop?

ASCII  ASCII stands for American Standard Code for Information Interchange (meaning it’s a way to communicate information)  In ASCII, each character, including numbers, punctuation, and white space characters are given a number, which is its ASCII value  You can see a full ASCII table at  There are two methods that you can use with ASCII:  ord(x) – gives you the ASCII value of x, a character  chr(x) – gives you the character that corresponds to x, an ASCII value  You can use ord and chr to do some very simple cryptography (ask Annie or me about it later if you want to know more)

Some useful string operators  + joins two strings together  Example: ‘soccer’ + ‘ball’ = ‘soccerball’  * repeats a string  Example: ‘cool’ * 3 = ‘coolcoolcool’  Indexing, works the same way indexing with lists does  word = ‘hello’, word[1] = ‘e’  Slicing  Used if you want to access a smaller section of a string; looks kind of like indexing  word[1:4] = ‘ell’ **NOTE: this did not return word[4]; slicing is exclusive: it goes up to, but does not include the second index

The string library  Just as there is a math library that you can use in your programs, there is also a string library that you can use  At the top of the program, you must type import string  This tells the computer that you want to see the functions contained in the string library file

More of the string library  Here’s a partial list of the functions contained in the string library:  string.capitalize(s) – capitalizes s (a string you would give the computer)  string.upper(s) – makes all the characters of s capital letters  string.strip(s) – removes all the white space (blank spaces, tabs, etc.) from the beginning and end of s  string.find(s, sub) – sub is a smaller string that you want to find within the larger string, s  string.count(s, sub) – counts the number of times sub occurs in s

Conditionals  Do you remember the work we did with logic gates yesterday?  Both of these are also used in Python  When might you want to use conditionals?

Boolean logic: a quick review  There are a few basic logic gates that we’re working with:  NOT: Requires one input, and makes the input opposite; turns true into false and false into true  OR: Requires two inputs, and as long as one input is true, then the OR gate returns true (or 1)  AND: Requires two inputs, and both must be true in order for the AND gate to be true (or 1)  Then there are the exclusive gates (which we don’t really have to worry about)  XOR: Works the same as OR, except that if both inputs are true, XOR is false  XAND: If one input is true, XAND is true. If both inputs are true, XAND is false  And last are the gates that combine NOT and either AND or OR  NAND: AND + NOT  NOR: OR + NOT For both of these, complete the OR or AND gate first, and then negate it using NOT

Some quick practice using the Python shell  Let’s open the Python shell and see this logic at work  Type in this: x = 5 then press Enter  Now type in x == 4 or x > 2 then press Enter. Did you get the result you were expecting?  Type in x == 5 and x > 8 then press Enter. Did you get the result you were expecting?

Conditional statements  Conditional statements are statements that depend on one or more conditions.  Conditional statements are used in programming when you want to complete a task, but only when certain conditions are met.  Example: You want to find the sum of two numbers, but only if they are both less than 5. How could you test that?  You (the programmer) set the condition, but the computer decides whether or not the condition is met!  There are a few different structures you can use with conditional statements in Python

If-statements  The simplest kind of conditional is a simple if- statement. Python evaluates the condition of the if- statement is true, the statement is completed. Otherwise, Python ignores it and moves on.  Syntax: if( ):  The condition can be anything you like, and there can be one or more than one. You can also use the logic gates OR and AND to test multiple conditions  Example:

If-else statements  If-else statements are used when you want the computer to perform an action even if the condition of your if-statement isn’t true.  For example: as long as two number are each less than five, find their sum. Otherwise, find their difference.  Coded:  Though an if-statement on its own will be ignored if its condition is false, an if-else statement will go straight to the else if the if statement’s condition is false  Notice that an else statement has no condition

If-elif-else statements  If-elif-else statements are used when there are three or more options  For example: A traffic light. IF the light is red, stop. ELSE IF the light is yellow, go. ELSE: go (because the only possibility left is that the light is green.)  The “elif” stands for else if  There is only ever one if statement and one else statement but there can be as many elif statements as you want.  Each condition is evaluated, beginning with the if and ending with the else. Whenever the computer reaches a statement for which the condition is true, it performs that code within that statements and then skips the rest of the statements

What structure would you use in each of these situations?  Determining where a point is on the Cartesian plane  If-elif-else  Determining the winner of a basketball game  If-else  Determining the number of days in a month  If-elif-else  Determining whether you passed a class or not  If-else

Your turn! Open the file called “Python – Lesson 4 Exercises” and complete the exercises