1 / 25

More Python!

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 d ays = [ “ Monday ” , “ Tuesday ” , “ Wednesday ” , “ Thursday ” , “ Friday ” , “ Saturday ” , “ Sunday ” ] print days[1]

jihan
Download Presentation

More Python!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 days = [“Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”, “Sunday”] print days[1] What will be printed?

  3. Decisions • 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 • We will explore how to implement this feature using the if statement

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

  5. Expression Tests

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

  8. The while loop • Take this sample program (From the tutorial) count=0 while count< 10: count=count+1 print count What will this program do?

  9. while loop • It will output 1 2 3 4 5 6 7 8 9 10

  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 • 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 • 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: For count in range(1,20): print count 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 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 • 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 < 7 and num2 > 10: print “congratulations! You’ve won a prize!” elif num >7 or num2 <10: print “sorry, you’ve lost the game!, follow instructions!” else: 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 • Functions typically put in a library so that they can be shared or saved for a rainy day when you need them again!

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

  19. Pseudocode • 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 • 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

  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, cont First, outline solutions to the problem using pseudo code without any worries about the specifics of Python or its syntax Second, write a short program…(very short) that uses some of the commands that we have discussed to solve the problem. You DO NOT have to use all the commands, just the ones that you need. Your program should contain comments for each line of code you write

  22. Hint: • You will of course need a loop! • Use variable names that make sense • List all variables at the beginning of the program and initialize them to some initial value

  23. More Hints • Just remember where you save it and what extension you use. The default folder is c:\python24 Remember the syntax rules and keep everything in lower case for consistency. Use the tutorials!!!!!

  24. The Rules • No Collaboration…write your own program!!….if you copy it from someone else, we will probably know it • If you really need help, go to section or TA hours….or come see me during office hours • This is actually an easy assignment once you think about it for a few minutes and review the material/tutorial

  25. Demo and questions

More Related