# You can use single or double quotes to create strings.location="Bloomington"# Concatenate strings with plus.where_am_i="I am in "+location# Print things with the print() function.print(location)print(where_am_i)# In Python everything is an object..# Strings have a lot of attached methods for common manipulations.print(location.upper())# Access single items with square bracket. Negative indices are from the back.print(location[0],location[-1])

# Lists use square brackets and are simple ordered collections of items (of arbitrary type).everything=[a,c,location,1,2,3,"hello"]print(everything)# Access elements with the same slicing/indexing notation as strings.# Note that Python indices are zero based!print(everything[0])print(everything[:3])print(everything[2:-2])print(everything[-3:])# Append things with the append method.# (Other helper methods are available for lists as well)everything.append("you")print(everything)

# Dictionaries have named fields and no inherent order. As is# the case with lists, they can contain items of any type.information={"name":"John","surname":"Doe","age":48,"kids":["Johnnie","Janie"]}# Acccess items by using the key in square brackets.print(information["kids"])# Add new things by just assigning to a key.print(information)information["music"]="jazz"print(information)

# Functions are defined using the "def" keyword.# The body of the function is the indented block following the# call syntax definition and usually ends with a "return" statement.defdo_stuff(a,b):returna*b# Functions calls are denoted by round brackets.print(do_stuff(2,3))

In [ ]:

# Arguments to functions can have a default value..deftraveltime(distance,speed=80.0):returndistance/speed# If not specified otherwise, the default value is used..print(traveltime(1000))

To use functions and objects not part of the default namespace, you have to import them. You will have to do this a lot so its worth to learn how to do it.

In [ ]:

# Import the math module, and use it's contents with the dot accessor.importmatha=math.cos(4*math.pi)# You can also selectively import specific things.frommathimportpib=2.0*pi# And even rename them if you don't like their name.frommathimportcosascosinec=cosine(b)print(c)

In automated processing scripts, sometimes unexpected things happen at runtime and Python stops code execution by raising an Exception (think of it as an "Error"). Sometimes we want to continue the program, nevertheless.

Large parts of the scientific Python ecosystem use NumPy. The heart of NumPy is the "ndarray" type (n-dimensional array, in our course usually 1-D arrays). Operations on NumPy arrays are both economical in memory use and computationally fast (internally computations are done in C).

In [ ]:

importnumpyasnp# Create a large array with with 1 million samples, equally spaced from 0 to 100x=np.linspace(0,100,1E6)# Most operations work per-element.y=x**2# Uses C and Fortran under the hood for speed.print(y.sum())

"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song's simple lyrics are as follows:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.

The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.

Your task here is to write a Python program capable of generating all the verses of the song.

In cryptography, a Caesar cipher is a very simple encryption techniques in which each letter in the plain text is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it to communicate with his generals. ROT-13 ("rotate by 13 places") is a widely used example of a Caesar cipher where the shift is 13. In Python, the key for ROT-13 may be represented by means of the following dictionary: