Useful/Basic Python Concepts - Likely Non Interview questions Flashcards
What are the ASCII ordinal values for 0, a, A ?
0 = 48
A = 65
a = 97
+ 32 to convert letter from capital to lowercase
- 32 to convert from letter lowercase to capital letter
How do you accept user input?
Use the input method: input(“Enter String”)
- You can save the input to a variable
- The input will always be a string but you can cast it to a different type such as an float or int
result = int(input(“Please enter a number: “))
How would you remove spaces from a string?
Use the replace method: string.replace(‘ ‘,’’)
string = string.replace(‘ ‘,’’)
What is an easy way to create a set of alphabetical characters?
Use string.ascii_lowercase or string.ascii_uppercase to make a list of the alphabet
CREATE AN STRING OF ALPHABET CHARACTERS IN LOWERCASE
alphabet = string.ascii_lowercase
CONVERT TO LIST
alphalist = list(alphabet)
CONVERT TO SET
alphaset = set(alphabet)
How could you print words in a sentence that starts with a specific letter?
- Use .split() method to split words in a sentence
- Then use slicing to grab the first letter of each word
- word[0]
- And finally use .lower() method to ensure the code handles words that that start with upper or lower case
st = ‘Print only the words that start with s in this sentence’
for word in st.split():
if word[0] == ‘s’:
print(word)
How could you retrieve all unique characters from a string?
Use the set method: set(string)
string = set(string)
How could you use list comprehension to create a list of numbers between 1 and 50 that are divisible by 3?
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
List comprehension syntax: newlist = [expression foritem initerable ifcondition ==True]
- Use range() to generate the numbers
- Use %3 test to check for numbers divisible by 3
[x for x in range(1,51) if x%3 == 0]
Fizz Buzz: Write a program that prints the integers from 1 to 100. But for multiples of three print “Fizz” instead of the number, and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
for num in range(1,101):
if num % 3 == 0 and num % 5 == 0:
print(“FizzBuzz”)
elif num % 3 == 0:
print(“Fizz”)
elif num % 5 == 0:
print(“Buzz”)
else:
print(num)
Fibonnaci Sequence: Write a program that prints the Fibonacci Sequence up to the given value.
Version using yield:
def fib(limit):
a,b = 0,1
while a < limit:
yield a
a, b = b, a + b
for x in fib(10):
print (x)
How would you provide default values into a function?
Add the default value to the input name as if you were setting a variable.
def say_hello(name=’Jason’):
print(f’Hello {name}’
How would you use a dictionary as counter variables and increment them as needed?
Increment as such: dict[‘key’] += 1
def up_low(s):
d = {‘upper’:0,’lower’:0}
for char in s: if char.isupper(): d['upper'] += 1 elif char.islower(): d['lower'] += 1 else: pass
How could you reverse a string?
Use slicing and -1.
string == string[::-1]
def palindrome(s):
# REMOVE SPACES IN STRING
s = s.replace(‘ ‘,’’)
# CHECK IF STRING == REVERSE STRING return s == s[::-1]
How could you Print or Return one of two options depending on boolean value?
Use two sets of square brackets, the first includes the two options and the second includes anything that evaluates to a bool (ex. An expression of some sort, 1 or 0, True or False)
If the second bracket includes False or 0 will then it will return/print the first option of the first bracket.
If the second bracket contains and evaluation of 1/True then it will print/return the second option of the first bracket.
Format: return [”Even”, “Odd”][num % 2]
def even_odd(num):
print([“1st”, “2nd”][num % 2])
print([“1st”, “2nd”][0]) #1st=0,False 2nd=1,True
print([“1st”, “2nd”][True]) # 1st=0,False 2nd=1,True
return [“Even”, “Odd”][num % 2]
even_odd(5)
output:
2nd
1st
2nd
Odd