Scientific Computing
Create python file practice.py
and save to cs100/ch4
. Copy this example code into it to follow along during class. Make sure you understand what each line is doing, and stop to ask questions if uncertain. As we walk through it together, write in comments what each function does.
# ----------- STRINGS -------------------
myString = "Hello CS 100 Students"
otherString = myString + ", how are you??"
print(otherString)
print( len(myString) )
print(myString[0])
print(myString[5])
smallString = "yep"
for i in range( len(smallString) ):
print( "index is: ", i, " -- item is: ", smallString[i] )
for item in smallString:
print( "item is: ", item )
numberString = "0123456789"
print( numberString[0:5] )
print( numberString[3:9] )
print( numberString[:4] )
print( numberString[5:] )
print( numberString[5:9] )
name = "heather"
print( "h" in name )
print( "z" in name )
print( "the" in name )
print( "z" * 50)
myString = "Hi CS 100!"
print( myString.center(50) )
print( myString.ljust(50) )
print( myString.rjust(50) )
words = "hello check check testing testing check check"
print( "number of e's: ", words.count("e") )
print( "number of 'check's: ", words.count("check") )
print( words.upper() )
print( words )
print( words.find("e") )
print( words.find("check") )
print( words.replace("e", "z") )
print( words )
print( ord('a') )
print( ord('b') )
print( chr(97) )
# --------- LIST ---------------
myList = ['a', 'b', 'c', 'd']
print( myList + ['e', 'f', 'g', 'h'] )
print( myList*3 )
numbers = list(range(10))
print(numbers)
words = ["hello", "my", "name", "is", "heather"]
print( words[0] )
print( words[2] )
for index in range( len(words) ):
print("index: ", index, " -- item: ", words[index] )
for item in words:
print(item)
print( "name" in words )
print( "hi" in words )
print( words[1:3] )
print( words[2:] )
print( words[:2] )
words[0] = "yo"
print(words)
words.append("!!!")
words.append("HI")
words.append("!!!")
words.append("!!!")
print(words)
words.pop()
print(words)
words.pop(0)
print(words)
if "name" in words:
print("index: ", words.index("name"))
else:
print("name is not in words")
print( "number of '!!!':", words.count("!!!") )
print( "number of 'HI':", words.count("HI") )
words.remove("my")
print(words)
myString = "hydrogen helium boron"
print( myString.split('o') )
print( myString.split(' ') )
myList = [6, 8, 2, 1, 4, 9, 10, 3]
myList.reverse()
print(myList)
myList.sort()
print(myList)
Submit practice.py
, complete with your comments, to Moodle.
Awesome! Here’s some other fun things you can do:
isPalindrome(myString)
to return True
or False
based on whether myString is a palindrome. A palindrome is a string such as "racecar"
which is the same forwards as it is backwards. For example:
print(isPalindrome("racecar")) # prints True
print(isPalindrome("madam")) # prints True
print(isPalindrome("yourname")) # prints False
isPalindrome
function also handles spaces. You may need to update your implementation to do so. For example
print(isPalindrome("a man a plan a canal panama")) # prints True
print(isPalindrome("never odd or even")) # prints True
print(isPalindrome("your name")) # prints False