Python day 1 recap
Contents
Question sheets
Page 1
1.) Option 1 and 2, option 3 is wrong (Python will see this as three variables with names enter, your and name separated by spaces - this gives a syntax error)
2.) Option 1, 3, 4 and 5. Option 2 is wrong - Python can't make an integer out of letters.
3.) Option 1 and 4. Option 2 is wrong - you cannot assign a value to an integer (it cannot be a variable name), option 3 is also wrong - when you assign the variable has to be on the left of the = sign.
4.) Option 1 and 4. Option 2 is wrong, you cannot use ., and option 3 is a command - you cannot make this a variable.
5.) Option 1, 2 and 3. Option 4 is a division by zero.
6.) Option 1 and 2 will result in an integer 8 (the // is the same as /), option 4 will result in a float 8.0, and option 3 will result in 16.
7.) Option 2 is the only 'real' comment - you can use option 1 as a comment but it's also a multi-line string. Option 3 will fail, option 4 is a string.
Page 2
- 1023 - int
- None - NoneType
- [2, 4, 8, 16] - list
- True - Boolean
- 17.54 - float
- ('Roger',1952) - tuple
- "my fat cat" - str(ing)
- {'name': 'Roger', 'birth': 1952} - dict(ionary)
Page 3
Starting from top, clockwise:
- False
- my fat rat
- 3
- cat
- my fat cat (NOTE: no newline!)
- MY FAT CAT
- t
- ['my','fat','cat']
Page 4
- 1 -> R.GB
- 2 -> 3. RGB
- 3 -> 3.14
- 4 -> B RG
- 5 -> 5
- 6 -> RG 7B
Page 5
Starting from top, clockwise:
- [0,1]
- 1
- [0,0,1,2]
- 0 (Note: default is to take last from list)
- 4
- True
- [2,0,1,0,"A"]
- 2
Page 6
Operations in order:
5, 1, 2, 4, 3
Page 7
Operations in order:
3, 5, 1, 6, 2, 4
Page 8
1.) Option 3
2.) Option 2. Option 1 multiplies the list, option 3 starts at 0, option 4 gives the wrong values (8 at the end)
1.) Options 1 and 3. In option 2 there's a comma missing, option 4 is a list
2.) Options 1 and 3.
3.) Options 1, 2, 3, and 4
Page 9
1.) Options 2, 3, 4 and 6. Option 1 is an assignment, 5 is missing :.
2.) Options 1, 2, 3.
3.) Options 1, 3.
4.) Options 1, 2, 3. Option 6 is correct if seq is a list/tuple/string
5.) Options 1, 2, 4, 5, 6
Page 10
- 1 -> a != 0
- 2 -> a < 19
- 3 -> abs(a) < 7
- 4 -> a >= 0
Exercise solution
# This variable will be used for the while loop validSequence = False # Keep on going as long as the DNA sequence is not valid while not validSequence: # Get a string from the user fullDnaSequence = input("Please enter your full DNA sequence:") fullDnaSequence = fullDnaSequence.upper() # Count the GACT characters in the sequence gactCount = 0 for code in 'GACT': gactCount += fullDnaSequence.count(code) # Check if the number of GACT characters matches the full length of the sequence # and set validSequence to True if so - this will stop the while: loop if gactCount == len(fullDnaSequence): validSequence = True else: print("\nInvalid sequence, only GACT allowed, try again!.\n") # Print some line breaks print("\n\n") # Prime the list to track the DNA fragments and the variable for the while loop dnaFragmentInfo = [] dnaFragment = input("Please give a DNA fragment to check:") while dnaFragment: # Check if present at all dnaFragmentCount = fullDnaSequence.count(dnaFragment) if dnaFragmentCount: currentDnaSequenceIndex = 0 for i in range(dnaFragmentCount): # Equivalent to #currentDnaSequenceIndex = currentDnaSequenceIndex + fullDna... currentDnaSequenceIndex += fullDnaSequence[currentDnaSequenceIndex:].index(dnaFragment) print("\n Fragment {} present at position {}.\n".format(dnaFragment,currentDnaSequenceIndex + 1)) dnaFragmentInfo.append((currentDnaSequenceIndex + 1,dnaFragment)) currentDnaSequenceIndex += 1 else: print("\n Fragment {} not present!\n".format(dnaFragment)) dnaFragment = input("Please give a DNA fragment to check:") # Print some line breaks print("\n\n") # Print out the fragment information again, first sort it dnaFragmentInfo.sort() for (dnaFragmentPosition,dnaFragment) in dnaFragmentInfo: print("Found {} at position {}".format(dnaFragment,dnaFragmentPosition))