Class 40 — December 4
Taking care of business
Take care of our world — For others need to follow — The path that you make
Look both ways
Please help out CS 1112
Agenda
- Chrestomathical and chrestohistorical
Gallery
Downloads
- The below code fragments can be found in scraps.py
Important patterns
- Print output
print( "ABCDE" )
x = 11
y = 12
z = x + y
print( x, "+", y, "+", z )
- Get input
reply = input( "Enter data to be manipulated: " )
- Convert input to data
reply = input( "Enter word: " )
w = reply.strip()
reply = input( "Enter integer: " )
n = int( reply )
reply = input( "Enter decimal: " )
n = float( reply )
reply = input( "Enter text: " )
words = reply.split()
reply = input( "Enter three integers: " )
sa, sb, sc = reply.split()
a, b, c = int( sa ), int( sb ), int( sc )
- Perform calculations
import math
reply = input( "Enter radius: " )
r = int( reply )
circumference = 2 * math.pi * r
area = math.pi * ( r ** 2 )
reply = input( "Enter integer: " )
n = int( reply )
d1 = n % 10
d2 = n // 10 % 10
- Process strings
reply = input( "Enter text: " )
s = reply.strip()
reply = input( "Enter key: " )
key = reply.strip()
no_caps_version = s.lower()
nbr_occurrences = s.count( key )
first_occurrence = s.find( key )
second_occurrence = s.find( key, first_occurrence + 1 )
slice = s[ first_occurrence : second_occurrence ]
- Get web input
import url
hangman_url = "http://www.cs.virginia.edu/~cs1112/words/hangman"
tough_words = url.get_contents( hangman_url )
- Examine and create lists
- Convert a string into a list of strings
reply = input( "Enter text: " )
words = reply.split()
- Convert a list of strings into a list of numbers
reply = input( "Enter integers: " )
numeric_strings = reply.split()
integers = []
for s in numeric_strings :
i = int( s )
integers.append( i )
- Perform an accumulation
sum = 0
product = 1
for i in integers :
sum = sum + i
product = product * i
series = []
for i in range( 0, n ) :
value = 2 ** i
series.append( value )
canonical_strings = []
for s in strings :
s = s.strip().lower()
canonical_strings.append( s )
- Check to see if the elements of the list are all of the same form
are_all_positive = True
for i in integers :
if ( i < 0 ) :
are_all_positive = False
break
- Check to see if any element of the list has a desired form.
some_are_positive = False
for i in integers :
if ( i >= 0 ) :
some_are_positive = True
break
- Generate random data
- Get a random value from a range
import random
i = random.randrange( 0, 100 )
- Select a random element from a list
import random
s = random.choice( strings )
- Produce a random sentence (gibberish)
import random
sentence = ""
for i in range( 0, 5 ) :
word = random.choice( words )
sentence = sentence + word + " "
sentence = sentence.strip()
- Produce a random tuple
import random
r = random.randrange( 0, 255 )
g = random.randrange( 0, 255 )
b = random.randrange( 0, 255 )
pixel = ( r, g, b )
- Index of first matching element
if key in integers :
i = integers.index( key )
else :
i = -1
- Compare two lists
pairwise_sorted = True
n = len( list1 )
for i in range( 0, n ) :
w1 = list1[ i ]
w2 = list2[ i ]
if ( w1 > w2 ) :
pairwise_sorted = False
break
- Synthesize two lists
n = len( list1 )
merged_list = []
for i in range( 0, n ) :
w1 = list1[ i ]
w2 = list2[ i ]
merged_list.append( w1 )
merged_list.append( w2 )
© 2019 Jim Cohoon | Resources from previous semesters are available. |