Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 More Python!

2 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?

3 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

4 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

5 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

6 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....

7 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!

8 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?

9 while loop It will output It will output12345678910

10 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

11 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!

12 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

13 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!

14 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

15 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

16 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! ”

17 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!

18 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)

19 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

20 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)

21 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!!)

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

23 QUESTIONS?


Download ppt "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."

Similar presentations


Ads by Google