Python Code Academy 2

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

I Got 99 Problems, But a Switch Ain't One

"Elif" is short for "else if." It means exactly what it sounds like: "otherwise, if the following expression is true, do this!" if 8 > 9: print "I don't get printed!" elif 8 < 9: print "I get printed!" else: print "I also don't get printed!" In the example above, the elif statement is only checked if the original if statement if False.

This and That (or This, But Not That!)

Boolean operators aren't just evaluated from left to right. Just like with arithmetic operators, there's an order of operations for boolean operators: not is evaluated first; and is evaluated next; or is evaluated last. For example, True or not False and False returns True. If this isn't clear, look at the Hint. Parentheses () ensure your expressions are evaluated in the order you want. Anything in parentheses is evaluated as its own unit.

To Be and/or Not to Be

Boolean operators compare statements and result in boolean values. There are three boolean operators: and, which checks if both the statements are True; or, which checks if at least one of the statements is True; not, which gives the opposite of the statement. We'll go through the operators one by one.

If You're Having...

Let's get some practice with if statements. Remember, the syntax looks like this: if some_function(): # block line one # block line two # et cetera Looking at the example above, in the event that some_function() returns True, then the indented block of code after it will be executed. In the event that it returns False, then the indented block will be skipped. Also, make sure you notice the colons at the end of the if statement. We've added them for you, but they're important.

Compare Closely!

Let's start with the simplest aspect of control flow: comparators. There are six: Equal to (==) Not equal to (!=) Less than (<) Less than or equal to (<=) Greater than (>) Greater than or equal to (>=) Comparators check if a value is (or is not) equal to, greater than (or equal to), or less than (or equal to) another value. Note that == compares whether two things are equal, and = assigns a value to a variable.

Pretty Time

Nice work! Let's do the same for the hour, minute, and second. from datetime import datetime now = datetime.now() print now.hour print now.minute print now.second In the above example, we just printed the current hour, then the current minute, then the current second. We can again use the variable now to print the time. Ins

Extracting Info

Notice how the output looks like 2013-11-25 23:45:14.317454. What if you don't want the entire date and time? from datetime import datetime now = datetime.now() current_year = now.year current_month = now.month current_day = now.day You already have the first two lines. In the third line, we take the year (and only the year) from the variable now and store it in current_year. In the fourth and fifth lines, we store the month and day from now.

The Big If Really great work! Here's what you've learned in this unit:

Really great work! Here's what you've learned in this unit: Comparators 3 < 4 5 >= 5 10 == 10 12 != 13 Boolean operators True or False (3 < 4) and (5 >= 5) this() and not that() Conditional statements if this_might_be_true(): print "This really is true." elif that_might_be_true(): print "That is true." else: print "None of the above." Let's get to the grand finale.

Hot Date

Remember that the % operator will fill the %s placeholders in the string on the left with the strings in the parentheses on the right. In the above example, we print 2014-02-19 (if today is February 19th, 2014), but you are going to print out 02/19/2014.

And

The boolean operator and returns True when the expressions on both sides of and are true. For instance: 1 < 2 and 2 < 3 is True; 1 < 2 and 2 > 3 is False.

Not

The boolean operator not returns True for false statements and False for true statements. For example: not False will evaluate to True, while not 41 > 40 will return False.

Or

The boolean operator or returns True when at least one expression on either side of or is true. For example: 1 < 2 or 2 > 3 is True; 1 > 2 or 2 > 3 is False.

Else Problems, I Feel Bad for You, Son...

The else statement complements the if statement. An if/else pair says: "If this expression is true, run this indented code block; otherwise, run this code after the else statement." Unlike if, else doesn't depend on an expression. For example: if 8 > 9: print "I don't printed!" else: print "I get printed!"

Getting the Current Date and Time

We can use a function called datetime.now() to retrieve the current date and time. from datetime import datetime print datetime.now() The first line imports the datetime library so that we can use it. The second line will print out the current date and time.

Grand Finale

We've managed to print the date and time separately in a very pretty fashion. Let's combine the two! from datetime import datetime now = datetime.now() print '%s/%s/%s' % (now.month, now.day, now.year) print '%s:%s:%s' % (now.hour, now.minute, now.second) The example above will print out the date, then on a separate line it will print the time. Let's print them all on the same line in a single print statement!

Go With the Flow Just like in real life, sometimes we'd like our code to be able to make decisions. The Python programs we've written so far have had one-track minds: they can add two numbers or print something, but they don't have the ability to pick one of these outcomes over the other. Control flow gives us this ability to choose among outcomes based off what else is happening in the program.

def clinic(): print "You've just entered the clinic!" print "Do you take the door on the left or the right?" answer = raw_input("Type left or right and hit 'Enter'.").lower() if answer == "left" or answer == "l": print "This is the Verbal Abuse Room, you heap of parrot droppings!" elif answer == "right" or answer == "r": print "Of course this is the Argument Room, I've told you that already!" else: print "You didn't pick left or right! Try again." clinic() clinic()

To print today's date

from datetime import datetime now = datetime.now() print now

Conditional Statement Syntax

if is a conditional statement that executes some specified code after checking if its expression is True. Here's an example of if statement syntax: if 8 < 9: print "Eight is less than nine!" In this example, 8 < 9 is the checked expression and print "Eight is less than nine!" is the specified code.


Kaugnay na mga set ng pag-aaral

Mood Disorders, CONVERSION DISORDER, post-traumatic stress disorder (PTSD), Chapter 15 Psychological Disorders, Psychology vocab ch. 12, psychological disorders, Schizophrenia, Psych: Module 4

View Set

Chapter 15: An Overview of Growth, Development, and Nutrition

View Set

3.13: Unit Test: How important Ideas are Expressed

View Set

Abeka Spanish I Test 9 (quarter exam)

View Set