Week 1-3

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/20

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

21 Terms

1
New cards

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’

2
New cards

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:]

3
New cards

What would be the outcome of these lines of code:

string = ‘hello’
t = tuple(hello)
print(t)

(‘h’, ‘e’, ‘l’, ‘l’, ‘o’)

4
New cards

How to define a tuple called ‘t’ with one single character: ‘a’

t = tuple('a',)

5
New cards

What are the symbols for quotient (DIV) and remainder (MOD) in python?

Quotient: ‘//’
Remainder: ‘%’

6
New cards

How do you use the ‘math’ module in python? (Allows ‘radians’ ‘sqrt’ etc. to be used)

import math

At the top of the code

7
New cards

Write a for statement to print: word = ‘Hello’ 1000 times

word = "Hello"
for i in range(1000):
    print(word)

8
New cards

Write a while statement to print: word = “Hello” 1000 times

word ="Hello"
i = 0
while i < 1000:
    print(word)
    i+=1

9
New cards

In 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)

10
New cards

How do you reverse:
sentence = ‘Hello, World!’

sentence = "Hello, World"
sentence_reversed = reversed(sentence)

11
New cards

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!') 

12
New cards

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)

13
New cards

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 matrix

14
New cards

Use 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

15
New cards

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) 

16
New cards

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}")

17
New cards

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]

18
New cards

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)]

19
New cards

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)

20
New cards

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)

21
New cards

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"]))