What is typecasting? Give an example.
Typecasting: converting a value from one data type to another.
int(x), str(6), float(6)
Write a function that generates a random float
import random
def generateRandomFloat():
newFloat = random.random()
return newFloat
sum = 0
for i in range(1, 11):
if i % 2 = 0:
sum += i
print(sum)
== not =
What does this code do and what is the output?
range(0,10,2)
[0, 2, 4, 6, 8]
What is a loop that never ends?
infinite loop
What would be the output of a 5//2 mathematical operation? What is the difference between using // vs /?
Output: 2
True division vs int division
Write a program that prints numbers from 1 to 10 using the while loop and then for a for loop.
i = 1
while i <= 10:
print(i)
i += 1
for i in range(1, 11):
print(i)
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
total_sum = 0
for i in range(numbers):
total_sum + numbers[i]
print("The sum is", total_sum)
range(len(numbers))
+= not +
myList = ['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig', 'grape']
x = 4
for i in range(x):
print(lst[i::2], end=" ")
['apple', 'cherry', 'elderberry', 'grape'] ['banana', 'date', 'fig'] ['cherry', 'elderberry', 'grape'] ['date', 'fig']
Given a list of nums=[10,20,30,40,50,60,70,80].
What would be the output if I ran nums[-3:-1]?
[60, 70]
What are bad or illegal variable naming practices in python.
Cannot be keywords, cannot start with number, no special characters, no spaces
return
1numInList
@#$%^&*^%$#
Get a list of numbers from the user. Create a deep copy of the list named deepCopy. Create another copy of the list that is a slice of the first 1/3rd of the list.
user_input = input("Enter a list of numbers separated by spaces: ")
numbers = user_input.split()
deepCopy = []
for i in range(len(numbers)):
deepCopy.append(numbers[i])
firstThird = numbers[:len(numbers)//3]
myString = "hello world"
letter_count = {}
for letter in myString:
if letter != " ":
letter_count[letter] = letter_count[letter] + 1
else:
letter_count[letter] = 1
print(letter_count)
myString = "hello world"
letter_count = {}
for letter in myString:
if letter != " ":
if letter in letter_count:
letter_count[letter] += 1
else:
letter_count[letter] = 1
print(letter_count)
what are a, b, c?
def fun(x):
y = "hello"
z = "maine"
return y, z, x
a, b, c = fun("cold")
a = "hello"
b = "maine"
c= "cold"
Write a function called nestedList() that takes two one-dimensional lists of length n as arguments and returns the two-dimensional list composed of n 2-element lists. Each containing the corresponding elements from the original lists.
For example, if the original lists are [1, 2, 3] and [a, b, c], the new list is
[[1, a], [2, b], [3, c]]
def nestedList(list1, list2):
tempList =[]
for i in range(len(list1)):
tempList.append([list1[i], list2[i]])
return tempList
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
result = nestedList(list1, list2)
print(result)
What is mode in a file? Describe 4 main modes.
'r' - read
'w' - write
'a' - append
'x' - create
Write a program to get a number from the user and determine all of the factors that go into the number. Print them out as a list.
Ex. The factors of 12 are: [1, 2, 3, 4, 6, 12]
num = int(input("Enter a number: "))
factors = []
for i in range(1, num + 1):
if num % i == 0:
factors.append(i)
print("The factors of", num, "are: ", factors)
numbers = [1, 2, 3, 4, 5]
filename = 'numbers.txt'
try:
file = open(filename, 'w')
for number in numbers:
file.write(number)
file.close()
except:
print("UH OH")
numbers = [1, 2, 3, 4, 5]
filename = 'numbers.txt'
try:
file = open(filename, 'w')
for number in numbers:
file.write(str(number) + '\n')
file.close()
except:
print("UH OH")
count = 0
for i in range(1, 5):
for j in range(i):
for k in range(j, i):
if (i + j + k) % 2 == 0:
print("x")
count += 1
print("Total:", count)
14
What is the difference between read, readlines and readline?
read returns a string of entire file
readlines returns a list of strings
readline returns one line in a string
Name two mutable datatypes and two immutable. What is the difference?
Mutable: list, dictionary - can be changed
Immutable: String, tuple - cannot be changed
Write a recursive function that adds all of the numbers given to the function, until the number is less than 10. Allow the user to enter the series of numbers to be totaled. If the series 12345 is entered, the output should be 15.
def sum_digits(num):
if num < 10:
return num
else:
return num % 10 + sum_digits(num // 10)
user_input = int(input("Enter a series of numbers: "))
result = sum_digits(user_input)
print(result)
def factorial(n):
return n * factorial(n)
print(factorial(5))
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))
What is the output?
def someNums(n):
if n == 0:
return 1
return someNums(n - 1) + someNums(n - 1)
print(someNums(5))
Bonus: Do you notice a pattern?
32
Write a program that prints a multiplication table for numbers 1 through 5 using nested for loops.
for i in range(1, 6):
for j in range(1, 6):
print(f"{i * j} ", end=" ")
print()