LSESU Applicable Maths Python Lesson 3

1/11/16

Today we will be learning about

  • Strings and string indexing
  • Reading files
  • Lists
  • List comprehensions

Recap from week 2

  • Functions

This is what the my_function you made could look like

def my_function(a,b):
    if a==b:
        return 'Arguments are the same'
    elif a>b:
        return True
    else a<b:
        return False
  • import statements and packages

Do you understand every part of this line, and where it would be in your own code?

import matplotlib.pyplot as plt

This is what the random_string you made could look like

def random_string():
    var = rand.randint(1,5)
    if var==1:
        return 'Random integer was 1'
    elif var==2:
        return 'Random integer was 2'    
    elif var==3:
        return 'Random integer was 3'    
    elif var==4:
        return 'Random integer was 4'    
    elif var==5:
        return 'Random integer was 5'
    else:
        return 'Something went wrong :('

Strings

You've seen strings before in both the first and second lesson and so far, we've skipped over what they are and what you can do with them. Now we are going to introduce the fundamentals of how to use this data type.

name = 'Python O'PythonFace'

A string is a variable of any number of characters (letters, numbers and other symbols) enclosed inside quotation marks. In Python there is no difference between single (' ') or double (" ") quotes. Above, name is the variable identifier name we use in the code, it has type string and the value of the variable is the string of characters on the right side of =.

Declaring strings


In [ ]:
# Everyone should know how to create (or "declare") a string by now
var = 'This is a string'
alphabet = 'abcdefghijklmnopqrstuvwxyz'

Indexing strings

We can access only specific elements of a string using square brackets [ ]. This way you can split a string up into more manageable parts. You might use this if you are given lots of news reports and you only want the first sentence to analyse.

Important: Python uses "zero based indexing" which is quite common in many languages. When you count up from the first element you always start on 0!


In [ ]:
# We can get only the first element of the alphabet
# Note that a is the 0th character in the string
first_letter = alphabet[0]
print(first_letter)

# To get the second letter we would do this
second_letter = alphabet[1]
print(second_letter)

# We can also get a range of characters in the string
first_five_letters = alphabet[0:5]
print(first_five_letters)

This is very close to what we did last week when we looked at for statements


In [ ]:
# You can see that when we run the for loop, Python looks at the indexes of the letters
# in the string to iterate over

for letter in alphabet:
    print(alphabet.index(letter))
    print(letter)
    print()

Question - How would you get the last element of the list?


In [ ]:
# How can we get the z from the alphabet variable?
print(alphabet[])

Formatting strings

Often when you are creating strings, you might want some part to be flexible or change in some way. You can do that with the .format() function.


In [ ]:
# This us how you read input from a user
name = input('Input name: ')

# Now use the {} to leave some parts of the string blank and fill them in later
var = 'Hello! My name is {}'.format(name)
print(var)

Question - Write some code which asks a user for their birthday day, month and year separately and print the result formatted together


In [ ]:
# TO DO - Make 3 input requests to the user and format the result together as 1 string


# END TO DO

Escape characters

String formatting includes special characters to make more complicated strings easy. These are always denoted with the \ character and are referred to as escape characters.

Common escape characters you might use will be newline \n or tab space \t. You also use the backslash to put quotes inside your string, like this \". If you are interested more in all the escape chars for Python look here


In [ ]:
n_string = 'This\nis\na\nmulti\nline\nstring\n'
print(n_string)

t_string = 'This\tstring\thas\ttab\tspaces\n'
print(t_string)

q_string = '\"This string is inside quotes\"'
print(q_string)

String methods

Some maths operations work on strings, and strings have some of their own methods which are specifically for working with variables made of characters.


In [ ]:
string1 = 'Hello'
string2 = 'Python'

# Add two strings together
print(string1 + string2)

# Another way to do this is using .join
print(' '.join([string1,string2]))

# Repetition using the * multiplication operator
print(string1*3) 

# Remember the membership `in` keyword
print('o' in string1)

# You can make all characters uppercase
print(string1.upper())

# Or all lowercase
print(string2.lower())

# You can also search for patterns and replace characters
string3 = 'I\'m really more of a cat person'
print(string3.replace('cat','dog'))

# You can check if a string of a number is a number or not
print('12345'.isdigit())

# Or check if a string is only made of alphabet characters
print('alphabet'.isalpha())

# Get the length of a string
print(len(alphabet))

Triple quotes and multi line strings


In [ ]:
# To make big strings you use three quote marks

big_string = '''Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say
                that they were perfectly normal, thank you very much. They were the last
                people you'd expect to be involved in anything strange or mysterious,
                because they just didn't hold with such nonsense.'''

# And Python will join strings declared over multiple lines together
multi_line_string = 'Hello my name is Tom ' \
                    'my favourite colour is Blue ' \
                    'and I live in Earl\'s Court'

print(multi_line_string)

A note on encoding: there are many different kinds of character encodings people have created. The standard for Python 3 is 'UTF-8' which isn't worth knowing until you try and read data which isn't in this encoding and then it gets messy. More info on encoding can be read here

File I/O

Now that we are more comfortable with strings, I'm going to introduce the basics of reading and writing to files. Since a string can include (almost) any character they are always the default data type that the contents of a file are turned into when you import the data. If its numeric data like stock price changes, it's up to you to convert it to integer or float values!

Opening a file for reading or writing

When you run any Python script and you want to read or write to a file, the starting point is the open() statement. This looks for the file in the location you specify and takes a second argument depending on if you want to read of write to the file:

  • open(FILEPATH,'r') - Reading from the file
  • open(FILEPATH,'rb') - Reading from the file and writing
  • open(FILEPATH,'w') - Writing to the file, will overwrite old data
  • open(FILEPATH,'a') - Appending to the file, will add new data to the end of old data

If you add a b to any of these args (i.e. rb) it will open the file in binary mode, this isn't used often but is safer sometimes if you don't know what type of data is in the file.


In [ ]:
# NOTE: This code won't work unless you make a file called `test_data.txt` 
# in the same folder as this lesson

file = open('test_data.txt','r')

# The file is opened and has a "handle" which is the file variable for interaction
type(file)

Reading files

There are two main ways to read the contents of a text file, either all at once or line by line


In [ ]:
file.read()

In [ ]:
file.readline()

Note: When you read from a file in Python, the program remembers where you ended last time you read the file, so if you try and readline() after read(), you have read the whole file so you don't get anything back

Reading line by line

In [ ]:
file = open('test_data.txt','r') # Reopen to set the file handle to the beginning of the text

# This loop calls .readline() automatically
for line in file:
    print(line)

Writing files

The .write() function does the opposite of .read(). If you give .write() a string to write, this will get put inside the file


In [ ]:
# You use open() to create a new file even if it did not already exist

file2 = open('write_data.txt','w+')

file2.write('this is a text file\n')
file2.write('test file for a python class\n')

# The number of chars written to the file is the return value

In [ ]:
# Since we used w+ we can read the file to check what we created
for line in file2:
    print(line)

Closing files

When you are done using your files, you should close them so that the Python programme no longer has access as so:


In [ ]:
# This is important for file security so nothing is corrupted

file.close()
file2.close()

Context managers

There is a way around using the close() statement called a Context Manager. These use the with statement to create a code block, and the opened file is only used inside the code block. When you exit the contexted managed code block, the file is automatically closed.


In [ ]:
# We could have read the file using a context manager like this

with open('test_data.txt','r') as file:
    # In this indented code block the file is open
    for line in file:
        print(line)
        
# No need to close the file! It's handled by the Python program now

We've breezed over File IO for now but if you want to look more in detail the TutorialsPoint page in this subject can give you more guidance if you are lost.

Lists

You have seen lists a few times before, but I haven't explained them until now. A list is a type of "Data Structure" i.e. a specific frame for organising items of data.

A list is declared as so:

list1 = [a,b,c,d,e,f]

Each item in the list is seperated by a comma, the data in each item can be any type, including another list. A list is an iterable object, and in fact you will come to see you can treat a string and a list very similarly! Just think a string as a list of characters

str = 'hello'
lis = ['h','e','l','l','o']

The other main Python data structures we will look at next week are tuples, dicts and sets.

Declaring a list


In [ ]:
# Declaring a list
list1 = [1,2,5,5,6]

# A list can have different types inside each element
list2 = list(('hello',4e-6,45))

print(list1)
print()
print(list2)

Indexing a list (it's just like indexing a string!)


In [ ]:
print(list1[0])

print(list2[1:3])

Iterating over a list


In [ ]:
# Exactly the same as reading a file or printing all the chars in a string
for elem in list2:
    print(elem)

Modifying a list

Elements of a list can be changed after declaration


In [ ]:
print(list1)
list1[-1] = 5000 # Use the -1 indexing the same as strings to get the end element
print(list1)

You can also delete elements and add to the end


In [ ]:
# Remove the end item
del list1[-1]
print(list1)

# Add a new element to the end
list1.append(6)
print(list1)

In [ ]:
# You can also use len() to get the size of a list
print(len(list1))

Problem - Print the list in reverse order using a for loop

Here's a tip


In [ ]:
# TO DO - Print this list in reverse order



# END TO DO

List comprehensions

Why use a comprehension?

These are one of my favourite features of the Python language. A list comprehension is a very readable way to run some operation on a list in one line of code. If you haven't quite understood today's lesson so far, get comfortable with that part first and revisit this later. You certainly won't ever NEED to use a list comprehension, but it will make for shorter code that shows you know what you are doing

Converting a list of temperatures

Imagine you have a list of temperatures in Celsius and you need to convert all the temperatures to Fahrenheit. The obvious way to do this is using a for loop


In [ ]:
celsius = [39.2, 36.5, 37.3, 37.8]
print(celsius)
fahrenheit = [] #Declaring an empty list

for temp in celsius:
    fahrenheit.append(float(9)/5*temp + 32)

print(fahrenheit)

This is a small example where the for loop code is only two lines so the advantage is small. But you can actually do all of this in 1 line!


In [ ]:
# The version using a list comprehension
fahrenheit2 = [float(9)/5*temp + 32 for temp in celsius]

print(fahrenheit2)

This is the basic version of the comprehension. You can also add conditions, like to only keep Celsius temperatures above 37


In [ ]:
fahrenheit3 = [float(9)/5*temp + 32 for temp in celsius if temp > 37]

print(fahrenheit3)

If we wanted to do this with loops we would have a for loop and an if statement. Now we have neither!

Multiplying two lists


In [ ]:
ones = [1,2,3,4,5]
tens = [10,20,30,40,50]

# The zip function puts each indexed element in pairs, like the teeth of a zip being locked together.
mult = [i*j for i,j in zip(ones,tens)]
print(mult)

Challenge - Write a comprehension to create a list of the first 10 square numbers?

Hint: Use the range function we looked at when we looked at for loops.


In [ ]:
# TODO


# END TODO