Python

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Inline comment print "I could have code like this." # my comment here

# Anything after the # is ignored by python.

Disable a line of code using #

# You can also use a comment to "disable" or comment out a piece of code: # print "This won't run."

formatter = "%r %r %r %r" print formatter % (1, 2, 3, 4) print formatter % ("one", "two", "three", "four") print formatter % (True, False, False, True) print formatter % (formatter, formatter, formatter, formatter) print formatter % ( "I had this thing.", "That you could type up right.", "But it didn't sing.", "So I said goodnight." )

1 2 3 4 'one' 'two' 'three' 'four' True False False True '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' '%r %r %r %r' 'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight.'

Variable assignment my_name = 'Zed A. Shaw' ( String) my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' my_age = 35 (integer) my_height = 74 my_weight = 180 # lbs

Can I make a variable like this: 1 = 'Zed Shaw'? No, 1 is not a valid variable name. They need to start with a character, so a1 would work, but 1 will not. Python knows you want something to be a string when you put either " (double-quotes) or ' (single-quotes) around the text.

print without quotes print(You take away its chair!)

File "main.py", line 3 print(You take away its chair!) SyntaxError: invalid syntax

# this line is tricky, try to get it exactly right print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)

If I add 35, 74, and 180 I get 289.

Using formatters to print print "Let's talk about %s." % my_name print "He's %d pounds heavy." % my_weight print "He's got %s eyes and %s hair." % (my_eyes, my_hair) print "His teeth are usually %s depending on the coffee." % my_teeth

Let's talk about Zed A. Shaw. He's 74 inches tall. He's 180 pounds heavy. Actually that's not too heavy. He's got Blue eyes and Brown hair. His teeth are usually White depending on the coffee. Note: you get TypeError: 'str' object is not callable. You probably forgot the % between the string and the list of variables.

print "Mary had a little lamb." print "Its fleece was white as %s." % 'snow' print "And everywhere that Mary went." print "." * 10 # what'd that do? end1 = "C" end2 = "h" end3 = "e" end4 = "e" end5 = "s" end6 = "e" end7 = "B" end8 = "u" end9 = "r" end10 = "g" end11 = "e" end12 = "r" # watch that comma at the end. try removing it to see what happens print end1 + end2 + end3 + end4 + end5 + end6, print end7 + end8 + end9 + end10 + end11 + end12

Mary had a little lamb. Its fleece was white as snow. And everywhere that Mary went. .......... Cheese Burger

In this example, the sum() method causes a rounding error to occur. The fsum() method returns a better sum. Python program that uses sum, fsum import math # Input list. values = [0.9999999, 1, 2, 3] # Sum values in list. r = sum(values) print(r) # Sum values with fsum. r = math.fsum(values) print(r)

Output 6.999999900000001 6.9999999

print "Is it greater?", 5 > -2 print "Is it greater or equal?", 5 >= -2 print "Is it less or equal?", 5 <= -2 print "What is 3 + 2?", 3 + 2 print "What is 5 - 7?", 5 - 7

Output: Is it greater? True Is it greater or equal? True Is it less or equal? False What is 3 + 2? 5 What is 5 - 7? -2

Why does the # not get ignored? print "Hi # there."

The # in that code is inside a string

What's the point of %s and %d when you can just use %r?

The %r is best for debugging, and the other formats are for actually displaying variables to users.

print "Mismatched quotes will cause a SyntaxError' print("How do you make a hot dog stand?') print ("Hello World!)

The program will abruptly stop running with the following message: File "script.py", line 1 print("How do you make a hot dog stand?') ^ SyntaxError: EOL while scanning string literal This means that a string wasn't closed, or wasn't closed with the same quote-character that started it.

1. x = "There are %d types of people." % 10 2. binary = "binary" 3. do_not = "don't" 4. y = "Those who know %s and those who %s." % (binary, do_not) 5. print x 6. print y 7. print "I said: %r." % x 8. print "I also said: '%s'." % y 9. hilarious = False 10. joke_evaluation = "Isn't that joke so funny?! %r" 11. print joke_evaluation % hilarious 12. w = "This is the left side of..." 13. e = "a string with a right side." 14. print w + e

There are 10 types of people. Those who know binary and those who don't. I said: 'There are 10 types of people.'. I also said: 'Those who know binary and those who don't.'. Isn't that joke so funny?! False This is the left side of...a string with a right side. Note: Why do you put ' (single-quotes) around some strings and not others? Mostly it's because of style, but I'll use a single-quote inside a string that has double-quotes. Look at line 10 to see how I'm doing that.

What is the difference between %r and %s?

Use the %r for debugging, since it displays the "raw" data of the variable, but the others are used for displaying to users.

print "What is 7.0/4.0?", 7.0/4.0

What is 7.0/4.0? 1.75 so use this method to get the answer as a float

print "What is 7/4?", 7/4

What is 7/4? 1 drops the fractional part after the decimal.

I get the error TypeError: not all arguments converted during string formatting.

You need to make sure that the line of code is exactly the same. What happens in this error is you have more % format characters in the string than variables to put in them. Go back and figure out what you did wrong.

Should I use %s or %r for formatting?

You should use %s and only use %r for getting debugging information about something. The %r will give you the "raw programmer's" version of variable, also known as the "representation."

x = (1 == True) y = (1 == False) a = True + 4 b = False + 10 print("x is", x) print("y is", y) print("a:", a) print("b:", b)

output x is True y is False a: 5 b: 10 we use boolean literal True and False. In Python, True represents the value as 1 and False as 0. The value of x is True because 1 is equal to True. And, the value of y is False because 1 is not equal to False. Similarly, we can use the True and False in numeric expressions as the value. The value of a is 5 because we add True which has value of 1 with 4. Similarly, b is 10 because we add the False having value of 0 with 10.


संबंधित स्टडी सेट्स

Allgemeine Psychologie 1 - 1. Lernen

View Set

Chemistry Final Exam (Chapter 9)

View Set

Frankenstein Chapter 6-8 Questions

View Set

New CA- Nutrition and cancer prevention

View Set

pre-calc radian/degree conversion

View Set

Employment Law: Chapter 5- Affirmative Action

View Set

Adult Health Chapter 49 Diabetes Mellitus EAQ Questions

View Set