Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = 4 + 8 We can get input from the user using.

Slides:



Advertisements
Similar presentations
Conditional Execution
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Python Programming Language
Introduction to Computing Science and Programming I
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Python November 14, Unit 7. Python Hello world, in class.
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.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Intro to Robots Conditionals and Recursion. Intro to Robots Modulus Two integer division operators - / and %. When dividing an integer by an integer we.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
An Introduction to Textual Programming
Line Continuation, Output Formatting, and Decision Structures CS303E: Elements of Computers and Programming.
Python Control Flow statements There are three control flow statements in Python - if, for and while.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
If statements while loop for loop
Flow of Control Part 1: Selection
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.
PROGRAMMING In Lesson 2. STARTER ACTIVITY Complete the starter activity in your python folder – lesson 2 Now we will see how you got on and update your.
Course A201: Introduction to Programming 09/16/2010.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris
Python Conditionals chapter 5
Decision Structures and Boolean Variables. Sequence Structures Thus far, we’ve been programming “sequence structures” Thus far, we’ve been programming.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Decision Structures, String Comparison, Nested Structures
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
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.
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.
Controlling Program Flow with Decision Structures.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
 Type Called bool  Bool has only two possible values: True and False.
Introduction to Decision Structures and Boolean Variables
Control Flow (Python) Dr. José M. Reyes Álamo.
Introduction to Python
ECS10 10/10
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.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Conditional Execution
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Selection CIS 40 – Introduction to Programming in Python
Python - Conditional Execution
And now for something completely different . . .
Conditions and Ifs BIS1523 – Lecture 8.
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Decision Structures, String Comparison, Nested Structures
Loops CIS 40 – Introduction to Programming in Python
Conditional Execution
Introduction to Decision Structures and Boolean Variables
Python Programming Language
Introduction to Value-Returning Functions: Generating Random Numbers
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
Class code for pythonroom.com cchsp2cs
Presentation transcript:

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 raw_input() >>>name = raw_input(“Enter your name”) We can change the data from one type to another >>>num = int(raw_input(“Enter a number”))

Conditional Statements So far the programs we’ve written are pretty boring Like with XSL we can use conditional statements to do more Basically conditional statements check to see if something is true. –If it is true, some code is executed –Perhaps if its not true, different code is executed Simplest conditional statement is the “if” statement

The If Statement The if statement in Python works just like the if statement in XSL –The syntax is different –Concept is the same If the number is less than 5, print “that number is less than 5” If the name is “C.S.Lewis”, print “this book is by C.S.Lewis”

If Statement, cont. num = int(raw_input(“Enter a number less than 10”)) if num>10: print “That number is bigger than 10. You can’t follow directions” print “Thanks for playing!”

If, cont. The syntax for the if statement is as follows: if expression: code The code you want executed if the expression is true must be indented –This is the body of your if statement Spacing is important in python –You should indent consistently –Convention is to indent 4 spaces (use the tab) When you stop indenting, the body of the if statement is considered to be done –The code after the indention will execute no matter what

In-Class Example Using if statements

Boolean Expressions When we use an if statement, the result of the expression is either true or false –if 4<3: (false) –if 3<4: (true) The result of the expression must be true or false Otherwise it doesn’t make sense These are called boolean expressions The two boolean values are “true” and “false”

Boolean Values, cont. Boolean values in python (like most languages) are stored as integers 0 represents false –4<3 as an expression has the value 0 Any other integer (usually 1) represents true –3<4 has the value of 1 You can actually write code like: if 1: …… if 5: …….

Boolean Expressions We’re pretty used to seeing, >= in conditional statements –They make sense and read just like regular English What if we want to check if something is equal to something else? –if 4-3 = 1: This is an assignment statement Does not check to see if 4 minus 3 equals 1 We need to use the == operator –if 4-3 ==1: If we want to check if two items are not equal use the != operator - if 4-2!=1:

Else Clause In XSL we can use,, and –This provides us with a sort of if, else if, else if….else structure Python has something similar We can use the else clause to provide a sort of “if not” If 3<4 then print “woohoo” else print “darn. I don’t know math” Code inside the else clause will be executed only if the expression in the if statement evaluates to false

If Else Example guess = int(raw_input(“Pick a number between 1 and 10”)) if guess<=10: print “Good job!” else: print “Can’t you follow directions!?”

In-Class Example Example using if/else Example using strings

elif block: Else if The if/else structure is pretty useful There is one more component to this structure: the “else if” block Basically we can now write code that reads something like: –if, else if, else if, …..,else To use an else if structure we need the word “elif” –elif requires an expression (just like if)

Elif Example if guess== number: print “you guessed it!” elif guess< number: print “that’s too low” else: print “that’s too big!”

In-Class Example with elif Changing the number guessing game to use elif Inventory with elif

We Can Nest “if” Statements We can nest if statements inside other if statements –Or inside elif statements –Or inside else statements –Basically anywhere Remember that nested ifs are only executed if the outer ifs are evaluated as true

In-Class nested If example Checking to be sure the number is within range

Random Number So far we have had to pre-select a number for the user to guess –num = 7 –It’s the same every time –Pretty boring once you guess the number It would be better if every time we ran the program it chose a random number for us to guess In order to use random numbers it requires two parts: –Importing the random module –Using a function from the random module to generate our random number

Random Numbers, cont. import random num = random.randint(1,10) Import tells python to include the random module –There are lots of modules –More on this later random.randint(1,10) –In the random module, use the randint function to select a random number between 1 and 10 –This is assigned to the variable num random.randint(5, 50) –Select a random integer between 5 and 50

In-Class Example Adding a random number to our guessing game

Libraries It would stink if every time we needed a random number we had to write the code to produce one Or how about something like calculating the sin of a number Or the square root With most programming languages some functions are included with the language –These are usually grouped into libraries –We only have to import the libraries or modules we need –If we imported every module it would take a long time to run In python, all of the prepackaged functions are considered to be part of the python library –The individual parts we will import will be modules or libraries Terms are pretty much interchangeable –Slightly different terminology from other languages

Modules When we needed the random number we had to import the random module The random module contains a function called randint() which is what actually gives us the random number The syntax for importing a module is: import moduleName We only import the modules we need

Accessing Functions in Modules To use a function found in a module we have to use the following syntax: moduleName.functionName(…) There are a lot of modules –There is a link to the reference on the course page Some of the common ones are random, math, and cmath We’ll be covering many more modules

In-Class Example Import the string module Use it to uppercase an entire string

Questions? What you should get from this lecture: –If statements –else clause –elif clause –Nesting if statements –How to produce a random integer –How to access functions in modules