more python!. lists, variables with more than one value variables can point to more than one value...
TRANSCRIPT
More Python!More Python!
Lists, Variables with more than Lists, Variables with more than one valueone value
Variables can point to more than one Variables can point to more than one value at a time. The simplest way to value at a time. The simplest way to do this is with a Listdo this is with a List
(also known as an Array)(also known as an Array)
Days = [Days = [““MondayMonday””, , ““TuesdayTuesday””, , ““WednesdayWednesday””, , ““ThursdayThursday””, , ““FridayFriday””, , ““SaturdaySaturday””, , ““SundaySunday””]]
print days[1]print days[1]
What will be printed?What will be printed?
DecisionsDecisions
The ability to make decisions is a The ability to make decisions is a key component of almost all programskey component of almost all programs
Decisions allow the program to Decisions allow the program to branch into different actions branch into different actions depending upon the result of a depending upon the result of a comparisoncomparison
We will explore how to implement We will explore how to implement this feature using the this feature using the if if statementstatement
The The ifif statement statement The basic syntax for the The basic syntax for the ifif statement is: statement is:
value = input (value = input (““enter an integer, enter an integer, please please ““))
if value < 45:if value < 45: print print ““value value is less than 45is less than 45””
else:else: print print ““value is 45 or largervalue is 45 or larger””
Basically, the program evaluates whether Basically, the program evaluates whether the expression value < 45 is true or falsethe expression value < 45 is true or false
Expression TestsExpression Tests
operator function
< 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 Multiple Tests in a single statementstatement
Consider this modification to the previous Consider this modification to the previous programprogram
value = input (value = input (““enter an enter an integerinteger””)) if value < 7:if value < 7:
print print ““value is less than 7value is less than 7””elif value == 7:elif value == 7:
print print ““value is equal to 7value is equal to 7”” else: else: print print ““value is value is
greater than 7greater than 7””ElifElif allows multiple tests to be performed allows multiple tests to be performed in a singlein a single if if statement.... statement....
The While Loop The While Loop
Ordinarily the computer interprets one line of Ordinarily the computer interprets one line of source code after another in sequencesource code after another in sequence
A control structure can change the order or A control structure can change the order or instruction execution or decide whether a instruction execution or decide whether a statement will be run at all depending upon statement will be run at all depending upon certain conditionscertain conditions
The The while control structurewhile control structure allows you to allows you to create a controlled program loopcreate a controlled program loop
A loop occurs when the program jumps backwards A loop occurs when the program jumps backwards on itself and re-executes previously executed on itself and re-executes previously executed instructionsinstructions
The loop must be controlled otherwise it can The loop must be controlled otherwise it can continue for an infinite amount of time!continue for an infinite amount of time!
The The while loopwhile loop
Take this sample programTake this sample program
count=0count=0
while count<10:while count<10:
count=count+1count=count+1
print countprint count
What will this program do?What will this program do?
while loopwhile loop
It will outputIt will output1122334455667788991010
The The while loopwhile loop
count=0count=0while count< 10:while count< 10:count=count+1count=count+1print countprint count
Notice that the variable Notice that the variable countcount is is initialized to a starting value. This is initialized to a starting value. This is very important to do with all your very important to do with all your variables before you use them to avoid variables before you use them to avoid unpredictable resultsunpredictable results
Avoid this problem!Avoid this problem!
We mentioned infinite loops earlierWe mentioned infinite loops earlier It is critical that at some point It is critical that at some point your while loop stops based on a your while loop stops based on a condition becoming true. Here is an condition becoming true. Here is an example of an infinite loop.example of an infinite loop.
count=1 count=1 while count==1:while count==1:print countprint count
There is no way for this loop to end since There is no way for this loop to end since the value of the value of countcount never changes! never changes!
the the for loopfor loop
Everything that can be done with the Everything that can be done with the while loopwhile loop can also be done with a can also be done with a for loop for loop and vice versaand vice versa
The The for loopfor loop is more direct when is more direct when dealing with a specific range or list dealing with a specific range or list of items that will control the number of items that will control the number of loops where the of loops where the while loopwhile loop is is better when the number of loops is better when the number of loops is undetermined or based on specific undetermined or based on specific inputs by the userinputs by the user
for loopsfor loops
Look at this examples:Look at this examples:
For count in range(1,20):For count in range(1,20):
print countprint count
Notice that we donNotice that we don’’t have to t have to change the value of count…it change the value of count…it happens automatically!happens automatically!
processing a variable processing a variable listlist
my_list = [my_list = [““hellohello””, 46, , 46, ““goodbyegoodbye””, , 12]12]
for item in my_list:for item in my_list:
print print ““The current item is: The current item is: ““,,
print itemprint item
Boolean ExpressionsBoolean Expressions
It is possible and practical to It is possible and practical to test conditions using Boolean test conditions using Boolean Expressions if you want to test two Expressions if you want to test two or more conditions at the same time or more conditions at the same time as part of an if/else construct as part of an if/else construct
As such you can combine the As such you can combine the boolean operators boolean operators ““andand”” , , ““oror””, , ““nonott ”” when performing a conditional when performing a conditional testtest
Boolean example using Boolean example using and / orand / or
num = input (num = input (““input a number between one and seven input a number between one and seven ““))
num2=input (num2=input (““input a number between ten and fifteen input a number between ten and fifteen ““))
if num < 7 if num < 7 andand num2 > 10: num2 > 10:
print print ““congratulations! Youcongratulations! You ’’ve won a prize!ve won a prize!””
elif num >7 elif num >7 oror num2 <10: num2 <10:
print print ““sorry, yousorry, you’’ve lost the game!, follow ve lost the game!, follow instructions!instructions!””
else:else:
print print ““Oops, I forgot to check for this Oops, I forgot to check for this condition!condition!””
FunctionsFunctions
In Python, functions are programs that In Python, functions are programs that are typically used to perform very are typically used to perform very specific operations that you may want specific operations that you may want to use many times. Rather than to use many times. Rather than repeating the code, calling a function repeating the code, calling a function is more efficientis more efficient
Functions typically get put in a Functions typically get put in a library so that they can be shared or library so that they can be shared or saved for a rainy day when you need saved for a rainy day when you need them again!them again!
FunctionsFunctions
defdef circumference(radius):circumference(radius):
circumference = (2*radius) * 3.14circumference = (2*radius) * 3.14
return circumferencereturn circumference
radius = input (radius = input (““enter radius enter radius ““))
print print ““the circumference is the circumference is ““, ,
print circumference(radius)print circumference(radius)
Remember to PseudocodeRemember to Pseudocode
Pseudocode is a means of writing out a program in Pseudocode is a means of writing out a program in natural language before you write the actual natural language before you write the actual source codesource code
It helps the programmer walk through the logic of It helps the programmer walk through the logic of a program and organize the designa program and organize the design
More time should be spent on design and less time More time should be spent on design and less time on actual writing and fixingon actual writing and fixing
Pseudo code should also be used to name all your Pseudo code should also be used to name all your variables and list their meaning if possiblevariables and list their meaning if possible
A pseudocode example using A pseudocode example using the previous programthe previous program
functionfunction
define function circumference with radius as define function circumference with radius as the inputthe input
circumference = 2 times the radius times 3.14circumference = 2 times the radius times 3.14
Return circumference valueReturn circumference value
Main programMain program
Input the radiusInput the radius
Print Print ““the circumference isthe circumference is””
Print value of function circumference(radius)Print value of function circumference(radius)
The assignmentThe assignment
The assignment should be read The assignment should be read several times to make sure you several times to make sure you understand itunderstand it
The problem will deal with writing a The problem will deal with writing a simple program to control a critical simple program to control a critical function in a state of the art function in a state of the art electric car (Like the Tesla!!)electric car (Like the Tesla!!)
Electric Cars are steadily increasing Electric Cars are steadily increasing in in
popularity…so the industry is popularity…so the industry is counting on you!counting on you!
QUESTIONS?QUESTIONS?