1/20
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
String Slicing: How would you slice the string (defined as ‘s’) ‘Hello World’ to get ‘Hello’ and then ‘World’ and then ‘Hello World’
s = "Hello World"
print(s[0:5]) or print(s[:5]) # - ‘Hello’
print(s[6:11]) or print(s[6:]) # - ‘World’
print(s[:]) # - ‘Hello World’Using string slicing, how would you change the string (defined as ‘s’) ‘Hello, World!’ to print ‘Yello, World!’
s = "Hello, World!"
s_new = "Y" + s[1:]What would be the outcome of these lines of code:
string = ‘hello’
t = tuple(hello)
print(t)
(‘h’, ‘e’, ‘l’, ‘l’, ‘o’)
How to define a tuple called ‘t’ with one single character: ‘a’
t = tuple('a',)What are the symbols for quotient (DIV) and remainder (MOD) in python?
Quotient: ‘//’
Remainder: ‘%’
How do you use the ‘math’ module in python? (Allows ‘radians’ ‘sqrt’ etc. to be used)
import math
At the top of the code
Write a for statement to print: word = ‘Hello’ 1000 times
word = "Hello"
for i in range(1000):
print(word)Write a while statement to print: word = “Hello” 1000 times
word ="Hello"
i = 0
while i < 1000:
print(word)
i+=1In the function: range(start, stop, step) - what do each of the parameters refer to?
start → the starting value (inclusive)
stop → the ending value (exclusive)
step → what to increment by (usually 1)
How do you reverse:
sentence = ‘Hello, World!’
sentence = "Hello, World"
sentence_reversed = reversed(sentence)Using the break statement, write code that takes user input until they write ‘done’
while True:
line = input('> ')
if line == 'done':
break
print('You wrote:', line)
print('Done!') Write code, using the continue statement, that executes the odd numbers from:
numbers = [1,2,3,4,5]
numbers = [1, 2, 3, 4, 5]
for num in numbers:
if num % 2 == 0:
continue # Skips the print() if its divisible by 2
print(num)Write code that prints a 2 × 3 matrix from the 2 dimensional list:
matrix = [[1, 2, 3],
[4, 5, 6]]
matrix = [[1, 2, 3],
[4, 5, 6]]
for row in matrix:
for element in row:
print(element, end=" ") # The 'end' seperates each by a white space
print() # Start a new line for the matrixUse the string method: ‘find’ to find where ‘na’ appears in the string
‘word = banana’
word = "banana"
index_1 = word.find("na")
index_2 = word.find("na", 3) #Tells to start at index 3
print(index_1, index_2)Would print ‘2’ and ‘4’ because that is where the index for both are
Write code using the ‘in’ operator, that locates the letters that are found in 2 words passed into the function ‘in_both’
def in_both(word1, word2):
for letter in word1:
if letter in word2:
print(letter) Using the f-string and the variables:
name = Ruby
age = 18
Write a line of code that returns the name and age of the person.
print(f"Hello, my name is {name} and my age is {age}")Write a line of code that concatenates the two lists:
[1, 2, 3] and [4, 5, 6]
lst_one = [1,2,3]
lst_two = [4,5,6]
new_lst = lst_one + lst_two
print(new_lst)Would return [1,2,3,4,5,6]
What does this line of code do?
list(zip('abc', [1,2,3]))
Creates 3 tuples using the ‘zip’ function, using each value in the string and list and sets each of the tuple pairs as values of a list. Would return
[(a,1),(b,2),(c,3)]
Write code that removes the white spaces from a sentence (using user input).
sentence = input("Enter a sentence: ")
output = ""
for letter in sentence:
if letter != " ":
output += letter
print(output)Write code that takes an input of a series of numbers (seperated by a white space, and as a string) and puts them into a list
numbers = input("Enter a series of numbers: ")
nums = numbers.split()
print(nums)Write a function that takes a list of numbers as a string and outputs them seperated by a white space
def to_nums(list):
string = " ".join(list)
print(string)
print(to_nums(["1", "2", "3", "4"]))