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.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computing Science and Programming I
Selection (decision) control structure Learning objective
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Basic Elements of Programming A VB program is built from statements, statements from expressions, expressions from operators and operands, and operands.
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.
Computer Science 1620 Programming & Problem Solving.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
ACSE th Conference The Iconic Programmer Stephen Chen.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Fundamentals of Python: From First Programs Through Data Structures
Python quick start guide
The University of Texas – Pan American
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
High-Level Programming Languages: C++
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Mastery Objective: Students will understand how to use while loops in computer programming.
VBScript Language. What is VBScript Based on the Visual Basic family of languages Supports object oriented features Interpreted Loosely Typed Implicitly.
CPSC 171 Introduction to Computer Science Algorithm Discovery and Design.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Algorithm Design.
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.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CS 240 – Computer Programming I Lab Kalpa Gunaratna –
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
Conditionals Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zPass by value limitations.
Algorithm Discovery and Design Objectives: Interpret pseudocode Write pseudocode, using the three types of operations: * sequential (steps in order written)
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
REPETITION CONTROL STRUCTURE
Python: Control Structures
IF statements.
Logical Operators and While Loops
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Expressions & Control Flow CSE 120 Winter 2018
Chapter 4: Control Structures I (Selection)
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Introduction to Object-Oriented Programming with Java--Wu
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
Loops CIS 40 – Introduction to Programming in Python
Coding Concepts (Basics)
Selection Statements.
Computer Science Core Concepts
ICT Programming Lesson 3:
Logical Operators and While Loops
The structure of programming
CHAPTER 5: Control Flow Tools (if statement)
Lecture 9: Implementing Complex Logic
COMPUTING.
Presentation transcript:

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 Variables can point to more than one value at a time. The simplest way to do this is with a List (also known as an Array) (also known as an Array) Days = [ “ Monday ”, “ Tuesday ”, “ Wednesday ”, “ Thursday ”, “ Friday ”, “ Saturday ”, “ Sunday ” ] Days = [ “ Monday ”, “ Tuesday ”, “ Wednesday ”, “ Thursday ”, “ Friday ”, “ Saturday ”, “ Sunday ” ] print days[1] print days[1] What will be printed?

Decisions The ability to make decisions is a key component of almost all programs The ability to make decisions is a key component of almost all programs Decisions allow the program to branch into different actions depending upon the result of a comparison Decisions allow the program to branch into different actions depending upon the result of a comparison We will explore how to implement this feature using the if statement We will explore how to implement this feature using the if statement

The if statement The basic syntax for the if statement is: The basic syntax for the if statement is: value = input ( “ enter an integer, please “ ) if value < 45: print “ value is less than 45 ” else: else: print “ value is 45 or larger ” print “ value is 45 or larger ” Basically, the program evaluates whether the expression value < 45 is true or false

Expression Tests operatorfunction < less than <= less than or equal to > greater than >= greater than or equal to == equal != not equal <> another way to say not equal

Multiple Tests in a single statement Consider this modification to the previous program value = input ( “ enter an integer ” ) if value < 7: print “ value is less than 7 ” elif value == 7: print “ value is equal to 7 ” else: print “ value is greater than 7 ” Consider this modification to the previous program value = input ( “ enter an integer ” ) if value < 7: print “ value is less than 7 ” elif value == 7: print “ value is equal to 7 ” else: print “ value is greater than 7 ” Elif allows multiple tests to be performed in a single if statement....

The While Loop Ordinarily the computer interprets one line of source code after another in sequence Ordinarily the computer interprets one line of source code after another in sequence A control structure can change the order or instruction execution or decide whether a statement will be run at all depending upon certain conditions A control structure can change the order or instruction execution or decide whether a statement will be run at all depending upon certain conditions The while control structure allows you to create a controlled program loop The while control structure allows you to create a controlled program loop A loop occurs when the program jumps backwards on itself and re-executes previously executed instructions A loop occurs when the program jumps backwards on itself and re-executes previously executed instructions The loop must be controlled otherwise it can continue for an infinite amount of time! The loop must be controlled otherwise it can continue for an infinite amount of time!

The while loop Take this sample program Take this sample program count=0 count=0 while count<10: while count<10: count=count+1 count=count+1 print count print count What will this program do?

while loop It will output It will output

The while loop count=0 while count< 10: count=count+1 print count Notice that the variable count is initialized to a starting value. This is very important to do with all your variables before you use them to avoid unpredictable results

Avoid this problem! We mentioned infinite loops earlier We mentioned infinite loops earlier It is critical that at some point your while loop stops based on a condition becoming true. Here is an example of an infinite loop. It is critical that at some point your while loop stops based on a condition becoming true. Here is an example of an infinite loop.count=1 while count==1: print count There is no way for this loop to end since the value of count never changes!

the for loop Everything that can be done with the while loop can also be done with a for loop and vice versa Everything that can be done with the while loop can also be done with a for loop and vice versa The for loop is more direct when dealing with a specific range or list of items that will control the number of loops where the while loop is better when the number of loops is undetermined or based on specific inputs by the user The for loop is more direct when dealing with a specific range or list of items that will control the number of loops where the while loop is better when the number of loops is undetermined or based on specific inputs by the user

for loops Look at this examples: Look at this examples: For count in range(1,20): print count Notice that we don ’ t have to change the value of count…it happens automatically! Notice that we don ’ t have to change the value of count…it happens automatically!

processing a variable list my_list = [ “ hello ”, 46, “ goodbye ”, 12] for item in my_list: print “ The current item is: “, print “ The current item is: “, print item print item

Boolean Expressions It is possible and practical to test conditions using Boolean Expressions if you want to test two or more conditions at the same time as part of an if/else construct It is possible and practical to test conditions using Boolean Expressions if you want to test two or more conditions at the same time as part of an if/else construct As such you can combine the boolean operators “ and ”, “ or ”, “ not ” when performing a conditional test As such you can combine the boolean operators “ and ”, “ or ”, “ not ” when performing a conditional test

Boolean example using and / or num = input ( “ input a number between one and seven “ ) num2=input ( “ input a number between ten and fifteen “ ) if num 10: print “ congratulations! You ’ ve won a prize! ” elif num >7 or num2 7 or num2 <10: print “ sorry, you ’ ve lost the game!, follow instructions! ” print “ sorry, you ’ ve lost the game!, follow instructions! ”else: print “ Oops, I forgot to check for this condition! ” print “ Oops, I forgot to check for this condition! ”

Functions In Python, functions are programs that are typically used to perform very specific operations that you may want to use many times. Rather than repeating the code, calling a function is more efficient In Python, functions are programs that are typically used to perform very specific operations that you may want to use many times. Rather than repeating the code, calling a function is more efficient Functions typically get put in a library so that they can be shared or saved for a rainy day when you need them again! Functions typically get put in a library so that they can be shared or saved for a rainy day when you need them again!

Functions def circumference(radius): circumference = (2*radius) * 3.14 circumference = (2*radius) * 3.14 return circumference return circumference radius = input ( “ enter radius “ ) print “ the circumference is “, print circumference(radius)

Remember to Pseudocode Pseudocode is a means of writing out a program in natural language before you write the actual source code Pseudocode is a means of writing out a program in natural language before you write the actual source code It helps the programmer walk through the logic of a program and organize the design It helps the programmer walk through the logic of a program and organize the design More time should be spent on design and less time on actual writing and fixing More time should be spent on design and less time on actual writing and fixing Pseudo code should also be used to name all your variables and list their meaning if possible Pseudo code should also be used to name all your variables and list their meaning if possible

A pseudocode example using the previous program function define function circumference with radius as the input circumference = 2 times the radius times 3.14 Return circumference value Main program Input the radius Print “ the circumference is ” Print value of function circumference(radius)

The assignment The assignment should be read several times to make sure you understand it The problem will deal with writing a simple program to control a critical function in a state of the art electric car (Like the Tesla!!) The problem will deal with writing a simple program to control a critical function in a state of the art electric car (Like the Tesla!!)

Electric Cars are steadily increasing in popularity…so the industry is counting on you!

QUESTIONS?