#! /usr/bin/env python # -*- coding: utf-8 -*- """ Solutions for the [first IntroToPython.org lecture](http://introtopython.org/var_string_num.html#exercises_variables). Credit: Lilian Besson for http://perso.crans.org/besson/cs101/solutions/intro/ License: GPLv3 """ ## ## Variables ## # + Hello World - variable # Store your own version of the message "Hello World" in a variable, and print it. # Example in French! message = "Bonjour le monde!" print message # Python supports UTF-8 encoding: your message can be written in Hindi or Telugu message_hindi = u"हैलो वर्ल्ड!" print message_hindi message_telugu = u"హలో ప్రపంచ!" print message_telugu # + One Variable, Two Messages: # Store a message in a variable, and then print that message. # Store a new message in the same variable, and then print that new message. message = "Good morning!" print message message ="Good afternoon!" print message message = "Good evening!" print message # And finally message = "Good night!!" print message ## ## Strings ## # + Someone Said # Find a quote that you like. Store the quote in a variable, with an appropriate introduction such as "Ken Thompson once said, 'One of my most productive days was throwing away 1000 lines of code'". Print the quote. name = "Bertrand Russell" # Cf. https://en.wikiquote.org/wiki/Bertrand_Russell quote = "What science cannot discover, mankind cannot know" # From Bertrand Russel's Religion and Science (1935), Ch. IX: Science of Ethics. print name, "once said, «", quote, "»." # + First Name Cases # Store your first name, in lowercase, in a variable. # Using that one variable, print your name in lowercase, Titlecase, and UPPERCASE. myname = "luc skywalker" print myname print myname.title() print myname.upper() # + Full Name # Store your first name and last name in separate variables, and then combine them to print out your full name. first_name = "Clark" last_name = "Kent" print first_name, last_name # Or with the '%' string formating operator print "%s %s" % (first_name, last_name) # Or with the .format() method print "{} {}".format(first_name, last_name) # + About This Person # Choose a person you look up to. Store their first and last names in separate variables. # Use concatenation to make a sentence about this person, and store that sentence in a variable. # Print the sentence. first_name = "ada" last_name = "lovelace" # We use the '+' operator used to concatene strings together name = first_name + " " + last_name sentence = name.title() + ", daughter of Lord Byron, is considered to be the world's first computer programmer. In 1843, she published a scientific paper containing the world's first computer program." print sentence # Cf. https://en.wikipedia.org/wiki/Ada_Lovelace # + Name Strip # Store your first name in a variable, but include at least two kinds of whitespace on each side of your name. # Print your name as it is stored. # Print your name with whitespace stripped from the left side, then from the right side, then from both sides. first_name = " sashank " print first_name # We see better the whitespaces with this print "'" + first_name + "'" print "'" + first_name.lstrip() + "'" # Whitespaces stripped from the left side print "'" + first_name.rstrip() + "'" # Whitespaces stripped from the right side print "'" + first_name.strip() + "'" # Whitespaces stripped from both sides ## ## Numbers ## # + Arithmetic # Write a program that prints out the results of at least one calculation for each of the basic operations: addition, subtraction, multiplication, division, and exponents. print 2 + 2 print 20 - 3 print 100 * 0.93 print 5 / 2 # Warning! print 0.5 / 0.025 print 2**8 # + Order of Operations # Find a calculation whose result depends on the order of operations. # Print the result of this calculation using the standard order of operations. # Use parentheses to force a nonstandard order of operations. Print the result of this calculation. print -5**2 # The standard order is exponent (**) before minus (-) print -(5**2) # We can force a nonstandard order with some parentheses print (-5)**2 # + Long Decimals # On paper, 0.1+0.2=0.3. But you have seen that in Python, 0.1+0.2=0.30000000000000004. # Find at least one other calculation that results in a long decimal like this. # It seems to be OK print "0.1 + 0.2 = ", 0.1 + 0.2 # But it is not mathematically correct, if we ask more digits print "0.1 + 0.2 = ", repr(0.1 + 0.2) # We can check this by asking (we will see more on if/else loops later) if 0.3 == 0.1 + 0.2: print "0.1 + 0.2 and 0.3 are equals: OK!" else: print "0.1 + 0.2 and 0.3 are differents ?!?" # Another example print "0.4 + 0.8 = ", repr(0.4 + 0.8) # + Python 2 or Python 3? # Use integer division to show whether your Python interpeter is using Python 2 or Python 3. if 2/3 is 0: print "2/3 = 0 so this is Python 2." else: print "2/3 = 0.66666666666 so this is Python 3." ## ## Challenges ## # + Neat Arithmetic # Store the results of at least 5 different calculations in separate variables. Make sure you use each operation at least once. # Print a series of informative statements, such as "The result of the calculation 5+7 is 12." result1 = 2 + 2 print "The result of the 1st calculation 2 + 2 is", result1 result2 = 20 - 3 print "The result of the 2nd calculation 20 - 3 is", result2 result3 = 100 * 0.93 print "The result of the 3rd calculation 100 * 0.93 is", result3 result4 = 5 / 2 print "The result of the 4th calculation 5 / 2 is", result4 result5 = 0.5 / 0.025 print "The result of the 5th calculation 0.5 / 0.025 is", result5 result6 = 2**8 print "The result of the 6th calculation 2**8 is", result6 # + Long Decimals - Pattern # On paper, 0.1+0.2=0.3. But you have seen that in Python, 0.1+0.2=0.30000000000000004. # Find a number of other calculations that result in a long decimal like this. Try to find a pattern in what kinds of numbers will result in long decimals. # If b=2a, apparently, a+b will result in long (and false) decimals ## ## Comments ## # + Choose the longest, most difficult, or most interesting program you have written so far. Write at least one comment in your program. # Well, this solution is a pretty good example of a well commented program.