Summer Scholars Introduction to Programming : Class Sequence - Monday, Tuesday
| Updated : June 13, 2015 |
Introduction to Python - in idle: Variables / place holders/ buckets statements assigning values, creating names, strings, numbers, printing single variables compuations with numbers vs error with computing strings strings vs numbers, converting string to integer and integer to string. All in idle Arithmetic operators : +, -, *, /, % [ remainder], ** (exponent) Printing multiple values with one print statement: using comma to separate variables in print statement using plus sign to separate variables using progam files (i.e. programs in files saved on hard drive ): Create file and store sequence of steps, importing libraries ( e.g. import sys) Write programs to do some of the things done in IDLE section above Print statements that do not go to a new line for next print end=”” Write programs that use several print statements to write on same line Print statements with more than one output line in same print statement “”” contents “”” Conditional statements if & else In IDLE Comparison operators >, <, == [ == vs = ] with numbers If statement , greater than >, less than < Create two variables with two different numeric values (i.e. numbers), Compare two variables, if first is larger than second print “first is largest” If/else: Compare two variables, if numbers are equal print “first is largest, print “first is largest”, if second is largest print “second is largest” Comparison operators == strings Create two string variables with two different strings Compare two variables, they are identical print “strings are identical”, else print strings are different. Test above comparison with identical strings and with different strings. If/else: Compare two variables, if numbers are equal print “first is largest, print “first is largest”, if second is largest print “second is largest”
In stored program: Given two numbers, write program to print smallest first, followed by largest. Should work regardless of values used for numbers. Num_1 = 82 Num_ 2 = 47 Given three numbers write program to print them out in order from highest to lowest. Var_1 = 100 Var_2 = 42 Var 3 = 57 Lists _1 in IDLE Create a list Print list Print specific indexed values in list Slices of list Print slices of list Create second list using slices of first list Append to list Delete from list Insert in list With stored programs Create list with 3 numbers Write program to create a second list with number sorted from largest to smallest Lists_2 In IDLE: Create an empty list Add 4 values to empty list Delete a value from the list Insert a value in the middle of the list Make two copies of list : one should share values with first list, second should be completely separate. Change 3rd value to different value Get length of list (i.e. number of values in list ) Create list with 3 sets : first name, age, weight, i.e. 9 values in list Print list Print all three ages Create a list of 3 lists : each of three list contains three items: first name, age, weight Print list Given a list containing 3 lists, print out the first of the three lists Print all three ages With stored programs: Create a list of 4 lists : each of four lists contains three items : first name, age, weight Print list Given the list containing 4 lists, print out the 3rd list of the 4 lists Print all four ages While loops In IDLE: Print numbers from 1 to 20 Print numbers from 6 to 19 Print every third number, and every 5th number between 1 and 100 Create list with 6 numbers Use while loop to print each number in list Use while loop to print each number from end of list back to first Create list with 3 sets : first name, age, weight, i.e. 9 values in list Print list Print all three ages Use a while loop to print all three ages Create a list of 3 lists : each list contains three lists of first name, age, weight Print list Print second list of three Print all three ages In program files: Write program similar to last thing done in IDLE, i.e.: Create a list of 5 lists : each sub-list contains three first name, age, weight Print list Print second list of three Use a while loop to print all five age ADVANCED Program: Write a program that will determine whether a number is a prime number or not. Helpful information : A prime number is one for which can only be divided evenly (i.e. no remainder) by one and itself The % operator will give the remainder from a division of integers: Here is a statement checking to see if a number can be divided evenly by another number: If ( (var_1%var_2)==0): print (var_1,”is not a prime, it can be divided evenly by”, var_2) You will need to use a while loop to search for possible divisors. If none are found, the number is a prime number.
Random numbers In IDLE Set values = to random numbers in ranges Create two separate random numbers Use if statement to print out the larger of the two numbers (“the larger number is “,… ) In stored program Write program to do two steps done in IDLE: Create two separate random numbers Use if statement to print out the larger of the two numbers (“the larger number is “,… ) Write a program using a while loop to print out ten random numbers Create list with 5 random numbers, no duplicates Write a program to create a second list with the numbers from the first list sorted in ascending order Write a program that will print “yes” or “no” randomly, but such that on average there will be as many “Yes” prints as “no” prints. Write a program that creates a list of colors [ “red”, “orange”, “yellow”, etc. ] and then prints out one the colors from the list in random order. Advanced program: Write a program that has three lists: Subjects of sentence (e.g. he, she, it, the ball, the horse…etc. ) Verb of sentence (use verbs with objects like hit, hits, pull, pulled, threw, found) Objects of verb action (the house, him, her, the frog etc. ) Then write code to make random sentences those three lists. Consider adding adjectives Consider sentences with verbs that use “to, for, from”, e.g. he runs from the house, the horse gives to her. How else could you make this interesting or potentially amusing when run Modify the above program so that (a) it never prints the same list value twice and (b) stops when all the values from the list are printed.
Advanced topics ( to be covered later in week ), could be learned with examples on summer web site or by Googling for Python code: User input with keyboard ( and removing the trailing “\n”) Get string from user Get number from user Write a program that does the following: Creates a random number between 1 an 100 Uses a loop to have user guess the number. Tells user if his guess is high or low. Optional: tell user if they are “very high” or “very low” (e.g. distance to number is greater than 35) How to use Python functions Use of Pyhon “Dictionaries”: Create and address book dictionary using last name as index File input/ouput Write a program to read and write to a text file |
||
In idle:
Variables / place holders/ buckets
statements assigning values, creating names, strings, numbers,
printing single variables
compuations with numbers vs error with computing strings
strings vs numbers, converting string to integer and integer to string. All in idle
Arithmetic operators : +, -, *, /, % [ remainder], ** (exponent)
Printing multiple values with one print statement:
using comma to separate variables in print statement
using plus sign to separate variables
using stored files :
Create file and store sequence of steps, with “import”
Write programs to do some of the things done in IDLE section above
Print statements that do not go to a new line for next print end=””
Write programs that use several print statements to write on same line
Print statements with more than one output line in same print statement “”” contents “””
Conditional statements if & else
In IDLE
Comparison operators >, <, == [ == vs = ] with numbers
If statement , greater than >, less than <
Create two variables with two different numeric values (i.e. numbers),
Compare two variables, if first is larger than second print “first is largest”
If/else:
Compare two variables, if numbers are equal print “first is largest, print “first is largest”, if second is largest print “second is largest”
Comparison operators == strings
Create two string variables with two different strings
Compare two variables, they are identical print “strings are identical”, else print strings are different.
Test above comparison with identical strings and with different strings.
If/else:
Compare two variables, if numbers are equal print “first is largest, print “first is largest”, if second is largest print “second is largest”
In stored program:
Given two numbers, write program to print smallest first, followed by largest. Should work regardless of values used for numbers.
Num_1 = 82
Num_ 2 = 47
Given three numbers write program to print them out in order from highest to lowest.
Var_1 = 100
Var_2 = 42
Var 3 = 57
Lists _1
in IDLE
Create a list
Print list
Print specific indexed values in list
Slices of list
Print slices of list
Create second list using slices of first list
Append to list
Delete from list
Insert in list
With stored programs
Create list with 3 numbers
Write program to create a second list with number sorted from largest to smallest
Lists_2
In IDLE:
Create an empty list
Add 4 values to empty list
Delete a value from the list
Insert a value in the middle of the list
Make two copies of list : one should share values with first list, second should be completely separate.
Change 3rd value to different value
Get length of list (i.e. number of values in list )
Create list with 3 sets : first name, age, weight, i.e. 9 values in list
Print list
Print all three ages
Create a list of 3 lists : each of three list contains three items: first name, age, weight
Print list
Given a list containing 3 lists, print out the first of the three lists
Print all three ages
With stored programs:
Create a list of 4 lists : each of four lists contains three items : first name, age, weight
Print list
Given the list containing 4 lists, print out the 3rd list of the 4 lists
Print all four ages
While loops
In IDLE:
Print numbers from 1 to 20
Print numbers from 6 to 19
Print every third number, and every 5th number between 1 and 100
Create list with 6 numbers
Use while loop to print each number in list
Use while loop to print each number from end of list back to first
Create list with 3 sets : first name, age, weight, i.e. 9 values in list
Print list
Print all three ages
Use a while loop to print all three ages
Create a list of 3 lists : each list contains three lists of first name, age, weight
Print list
Print second list of three
Print all three ages
In program files:
Write program similar to last thing done in IDLE, i.e.:
Create a list of 5 lists : each sub-list contains three first name, age, weight
Print list
Print second list of three
Use a while loop to print all five age
ADVANCED Program:
Write a program that will determine whether a number is a prime number or not.
Helpful information :
A prime number is one for which can only be divided evenly (i.e. no remainder) by one and itself
The % operator will give the remainder from a division of integers: Here is a statement checking to see if a number can be divided evenly by another number:
If ( (var_1%var_2)==0): print (var_1,”is not a prime, it can be divided evenly by”, var_2)
You will need to use a while loop to search for possible divisors. If none are found, the number is a prime number.
Random numbers
In IDLE
Set values = to random numbers in ranges
Create two separate random numbers
Use if statement to print out the larger of the two numbers (“the larger number is “,… )
In stored program
Write program to do two steps done in IDLE:
Create two separate random numbers
Use if statement to print out the larger of the two numbers (“the larger number is “,… )
Write a program using a while loop to print out ten random numbers
Create list with 5 random numbers, no duplicates
Write a program to create a second list with the numbers from the first list sorted in ascending order
Write a program that will print “yes” or “no” randomly, but such that on average there will be as many “Yes” prints as “no” prints.
Write a program that creates a list of colors [ “red”, “orange”, “yellow”, etc. ] and then prints out one the colors from the list in random order.
Advanced program:
Write a program that has three lists:
Subjects of sentence (e.g. he, she, it, the ball, the horse…etc. )
Verb of sentence (use verbs with objects like hit, hits, pull, pulled, threw, found)
Objects of verb action (the house, him, her, the frog etc. )
Then write code to make random sentences those three lists.
Consider adding adjectives
Consider sentences with verbs that use “to, for, from”, e.g. he runs from the house, the horse gives to her.
How else could you make this interesting or potentially amusing when run
Modify the above program so that (a) it never prints the same list value twice and (b) stops when all the values from the list are printed.
Advanced topics ( to be covered later in week ), could be learned with examples on summer web site or by Googling for Python code:
User input with keyboard ( and removing the trailing “\n”)
Get string from user
Get number from user
Write a program that does the following:
Creates a random number between 1 an 100
Uses a loop to have user guess the number.
Tells user if his guess is high or low.
Optional: tell user if they are “very high” or “very low” (e.g. distance to number is greater than 35)
How to use Python functions
Use of Pyhon “Dictionaries”:
Create and address book dictionary using last name as index
File input/ouput
Write a program to read and write to a text file