Select Page

List

As the name suggests, a list stores a collection of similar data types. List allow duplicate values.

fruits = ['orange', 'apple', 'pear', 'apple', 'banana']
#adds 'kiwi' to the end of the list
fruits.append('kiwi')
print(fruits)
print(len(fruits)) #prints 6 which is count of the list.
print(fruits[0]) #prints orange
print(frouts[3]) #prints apple
fruits.sort() #sorts the list in ascending order
fruits.pop()

Set

Set is similar to list, but does not allow duplicate values.

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket) # show that duplicates have been removed
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket# fast membership testing
True
>>> 'crabgrass' in basket
False

Repeat Statements

You can repeat one or more statements using for statement or while statement. One or more indented statements make a code block.

For statement

#Prints 0 1 2. The code block repeats 3 times
for i in range(0,3):
   print(i)
#Prints 0 2 4. The code block repeats 3 times
for i in range(0,6,2):
   print(i)
#Reverse count. Prints 3, 2, 1. Repeats 3 times
for i in range(3,0,-1):
   print(i)
#Iterates throw a string list
fruits=["apple", "banana", "pineapple"]
for fruit in fruits:
   print(fruit)
#Iterates each character in a string
#prints G O O D
s="GOOD"
for c in s:
   print(c)

While statement

#Repeat until user enters "exit" is entered. 
keep_going=true
while keep_going:
   name=input("You name? Enter exit to quit")
   if name=="exit":
      keep_going=False
   else:
      print("Hello", name)

Code Samples

Simple adding machine

This adder can only add integers. You learn to convert string to integer and to do simple arithmetic expressions.

#This is a simple adding machine
print(“Simple Adding Machine”)
s=input(“Enter first number: “)
n1=int(s)
s=input(“Enter second number: “)
n2=int(s)
result=n1+n2
print(“Sum of”, n1, “and”, n2, “is”, result)

This adder converts string to float.

#This is a simple adding machine
print(“Simple Adding Machine”)
s=input(“Enter first number: “)
n1=float(s)
s=input(“Enter second number: “)
n2=float(s)
result=n1+n2
print(“Sum of”, n1, “and”, n2, “is”, result).

Roll a dice

You learn to generate random numbers

#This program generates random numbers
import random
while True:
 input("Press any key to roll the dice...")
 dice_value=random.randint(1,6)
 print("You rolled a", dice_value)

Simple for loop

print('First for statement')
for i in range(1,10):
 print(i)
 print("the index is", i)

Funny sentence generator

#funny sentance generator
import random
subjects=["I", "We", "They"]
verbs=["ate", "drank", "drove"]
nouns=["milk", "a car", "cookies"]

while True:
 rnd_subject_index=random.randint(0, len(subjects)-1)
 rnd_verb_index=random.randint(0, len(verbs)-1)
 rnd_noun_index=random.randint(0, len(nouns)-1)
 s=input("Press any key to continue. Type end to exit")
 rnd_subject=subjects[rnd_subject_index]
 rnd_verb=verbs[rnd_verb_index]
 rnd_noun=nouns[rnd_noun_index]
 print(rnd_subject, rnd_verb, rnd_noun)

Multiplication table

n1=1
n2= 9
for n1 in range(1, 11):
  print(n1,' X ',n2,' = ',n2*n1) 

Our students got inspired by Alexa. In the next class we will attempt speech to text and text to speech.