python (yay!)

Download Python (yay!)

Post on 31-Dec-2015




0 download

Embed Size (px)


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 raw_input() >>>name = raw_input(“Enter your name”) We can change the data from one type to another - PowerPoint PPT Presentation


  • Python (yay!)November 16, Unit 7

  • RecapWe can store values in variables using an assignment statement>>>x = 4 + 8We 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 StatementsSo far the programs weve written are pretty boringLike with XSL we can use conditional statements to do moreBasically conditional statements check to see if something is true. If it is true, some code is executedPerhaps if its not true, different code is executedSimplest conditional statement is the if statement

  • The If StatementThe if statement in Python works just like the if statement in XSLThe syntax is differentConcept is the sameIf the number is less than 5, print that number is less than 5If 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 cant follow directionsprint Thanks for playing!

  • If, cont.The syntax for the if statement is as follows:if expression:codeThe code you want executed if the expression is true must be indentedThis is the body of your if statementSpacing is important in pythonYou should indent consistentlyConvention is to indent 4 spaces (use the tab)When you stop indenting, the body of the if statement is considered to be doneThe code after the indention will execute no matter what

  • In-Class ExampleUsing if statements

  • Boolean ExpressionsWhen we use an if statement, the result of the expression is either true or falseif 4
  • Boolean Values, cont.Boolean values in python (like most languages) are stored as integers0 represents false4
  • Boolean ExpressionsWere pretty used to seeing = in conditional statementsThey make sense and read just like regular EnglishWhat if we want to check if something is equal to something else?if 4-3 = 1:This is an assignment statementDoes not check to see if 4 minus 3 equals 1We need to use the == operatorif 4-3 ==1:If we want to check if two items are not equal use the != operator- if 4-2!=1:

  • Else ClauseIn XSL we can use , , and This provides us with a sort of if, else if, else if.else structurePython has something similarWe can use the else clause to provide a sort of if notIf 3
  • If Else Exampleguess = int(raw_input(Pick a number between 1 and 10))if guess
  • In-Class ExampleExample using if/else Example using strings

  • elif block: Else ifThe if/else structure is pretty usefulThere is one more component to this structure: the else if blockBasically we can now write code that reads something like:if, else if, else if, ..,elseTo use an else if structure we need the word elifelif requires an expression (just like if)

  • Elif Exampleif guess== number:print you guessed it!elif guess< number:print thats too lowelse:print thats too big!

  • In-Class Example with elifChanging the number guessing game to use elifInventory with elif

  • We Can Nest if StatementsWe can nest if statements inside other if statementsOr inside elif statementsOr inside else statementsBasically anywhereRemember that nested ifs are only executed if the outer ifs are evaluated as true

  • In-Class nested If exampleChecking to be sure the number is within range

  • Random NumberSo far we have had to pre-select a number for the user to guessnum = 7Its the same every timePretty boring once you guess the numberIt would be better if every time we ran the program it chose a random number for us to guessIn order to use random numbers it requires two parts:Importing the random moduleUsing a function from the random module to generate our random number

  • Random Numbers, cont.import randomnum = random.randint(1,10)

    Import tells python to include the random moduleThere are lots of modulesMore on this laterrandom.randint(1,10)In the random module, use the randint function to select a random number between 1 and 10This is assigned to the variable numrandom.randint(5, 50)Select a random integer between 5 and 50

  • In-Class ExampleAdding a random number to our guessing game

  • LibrariesIt would stink if every time we needed a random number we had to write the code to produce oneOr how about something like calculating the sin of a numberOr the square rootWith most programming languages some functions are included with the languageThese are usually grouped into librariesWe only have to import the libraries or modules we needIf we imported every module it would take a long time to runIn python, all of the prepackaged functions are considered to be part of the python libraryThe individual parts we will import will be modules or librariesTerms are pretty much interchangeableSlightly different terminology from other languages

  • ModulesWhen we needed the random number we had to import the random moduleThe random module contains a function called randint() which is what actually gives us the random numberThe syntax for importing a module is:import moduleNameWe only import the modules we need

  • Accessing Functions in ModulesTo use a function found in a module we have to use the following syntax:moduleName.functionName()There are a lot of modulesThere is a link to the reference on the course pageSome of the common ones are random, math, and cmathWell be covering many more modules

  • In-Class ExampleImport the string moduleUse it to uppercase an entire string

  • Questions?What you should get from this lecture:If statementselse clauseelif clauseNesting if statementsHow to produce a random integerHow to access functions in modules