A soft intro to Python (part II)

Homework :)

Generate a list of integers, and then show:

  • the list

  • the list in column format, displaying matching keys and values

  • the sum of all elements of the list

  • create a new list made of 5 multiples of the original list

  • give min and max values of the list

  • count occurrences of even and odd numbers and sum them

You can find all answers on this shared Colab.

Try it out !

Colab.

If you haven’t already, please create a Colab on your google account.

You’ll be able to copy paste the code from the slides and run them in your own Colab environment. And of course run your own code !

Generate a list of integers

  1. The list:
# I didn't specify anything, so this is valid:
mylist = list(range(11, 20))

# show list
mylist
[11, 12, 13, 14, 15, 16, 17, 18, 19]
# But you *could* use the random library:
import random
randlist = random.sample(range(11, 50), 9)

# show list
randlist
[23, 21, 26, 44, 11, 39, 36, 42, 16]

Generate a list of integers

  1. The list in column format:
# the list displaying matching keys and values
length = len(randlist)
for i in range(length):
  print(i, ":", randlist[i])
0 : 23
1 : 21
2 : 26
3 : 44
4 : 11
5 : 39
6 : 36
7 : 42
8 : 16
  1. The sum of all elements of the list, and the minimum and maximum values of the list
print("sum randlist = ", sum(randlist), "; max randlist = ", max(randlist), "; min randlist = ", min(randlist))
sum randlist =  258 ; max randlist =  44 ; min randlist =  11

Generate a list of integers

  1. Create a new list made of 5 multiples of the original list
listx5 = []

for i in randlist:
  listx5.append(i*5)
print("multiples list : ", listx5)
multiples list :  [115, 105, 130, 220, 55, 195, 180, 210, 80]

Generate a list of integers

  1. Count odd and even numbers
neven = 0
nodd = 0
for i in randlist:
  if i%2 == 0:
    neven += 1
  else:
    nodd += 1
print("randlist has", neven, "even numbers and", nodd, "odd numbers.")
randlist has 5 even numbers and 4 odd numbers.

.

.

.

Tudo bem ??

Let’s write some functions…

Let’s write some functions…

Syntax: use def and, if you want, return keywords.

Also, note INDENT, and the :

def question():
  a = input("How many coffees did you drink today?")
  return a

question()

Arguments: if you specify any, then you must use them in the function call:

def add_two(a) :
  return a + 2

add_two(50) 
# 52

Otherwise you’ll get an error:

add_two() 
# ERROR: "add_two() missing 1 required positional argument: 'a' "

Let’s write some functions…

Same thing for multiple arguments: you need to specify them all when you use your function.

def add_percent(a, b):
  return a + (a/100*b)

add_percent(50) # ERROR

Luckily, you can set default argument values. In that case, you don’t have to use all args in your function call:

def add_percent(a, b=30):
  return a + (a/100*b)

add_percent(50)
65.0

Overwrite default arg:

def add_percent(a, b=30):
  return a + (a/100*b)

add_percent(50, 10)
55.0

Practice!

Rewrite the last Homework exercise as a function:

Generate a list of integers, count occurrences of pair and odd numbers and then sum them.

This was the general solution:

import random
randlist = random.sample(range(11, 50), 9)

# show list
randlist
[34, 14, 12, 36, 49, 41, 48, 29, 13]
neven = 0
nodd = 0
for i in randlist:
  if i%2 == 0:
    neven += 1
  else:
    nodd += 1
print("randlist has", neven, "even numbers and", nodd, "odd numbers.")
randlist has 5 even numbers and 4 odd numbers.

Practice!

Now here is a function version of the previous:

# create list

def create_list(n_min, n_max, n_length):
  import random
  randlist = random.sample(range(n_min, n_max), n_length)
  return(randlist)
  
l = create_list(0, 100, 5)
print(l)
[81, 26, 73, 98, 89]
# count even and odd numbers:

def count_even(mylist):
  neven = 0
  nodd = 0
  for i in mylist:
    if i%2 == 0:
      neven += 1
    else:
      nodd += 1
  return(print("my list has", neven, "even numbers and", nodd, "odd numbers."))

count_even(l)
my list has 2 even numbers and 3 odd numbers.

Next time:

Importing data