CS-104 - Homework 1 Solutions PDF

Title CS-104 - Homework 1 Solutions
Course Comput Prog & Graph Prob
Institution New Jersey Institute of Technology
Pages 3
File Size 65.4 KB
File Type PDF
Total Downloads 81
Total Views 138

Summary

Homework 1, with solutions...


Description

Homework 1 Solutions Computer Programming and Graphics Problems CS 104 – 005 2.12- Write Python expressions corresponding to these statements: (b.) The average age of Sara (age 65), Fatima (57), and Mark (age 45) Age1=65 Age2=57 Age3=45 AverageAge=(Age1+Age2+Age3)/3 print(AverageAge) 55

(c.) 2 to the 20th power print(2**20)

1048576 (e.) The remainder when 4365 is divided by 61 print(4365%61) 34

2.13- Start by evaluating, in the interactive shell, the assignment: >>>s1 = '-' >>>s2 = '+' Now write string expressions involving s1 and s2 and string operators + and * that evaluate to: (b.)'-+' s1='-' s2='+' print(s1+s2)

(c.)'+--' x=s2+s1*2 print(x)

(e.)'+--+--+--+--+--+--+--+--+--+--+' print(x*10+s2)

2.14- Start by running in the shell the following assignment statement: >>>s= 'abcdefghijklmnopqrstuvwxyz' Now write the expressions using string s and the indexing operator that evaluate to 'a', 'c', 'z', 'y', and 'q' s='abcdefghijklmnopqrstuvwxyz'

print(s[0],s[2],s[25],s[24],s[16]) 2.15- Start by executing s='goodbye' Then write a Boolean expression that checks whether: (b.) The seventh character of s is g s='goodbye'

print(s[6]=='g') False (e.) The middle character of s is d print(s[3]=='d') True (f.) The first and last characters of string s are equal print(s[0]==s[6]) False 2.16- Write the corresponding Python assignment statements: (c.)Assign to variable inventory the list containing strings 'paper', 'staples', and 'pencils'. inventory = ['paper','staples','pencils'] (d.) Assign to variables first, middle, and last the strings 'John', 'Fitzgerald', and 'Kennedy' first= 'John' middle= 'Fitzgerald' last= 'Kennedy'

fullname=(first+' '+ middle+' '+last) 2.17- Write Boolean expressions corresponding to the following logical statements and evaluate the expressions: (b.) The length of list inventory is more than five times the length of string fullname len(inventory) > (len(fullname))*5 False

(e.) The length of string middle is larger than the length of string first and smaller than the length of string last print(len(first)...


Similar Free PDFs