Useful/Basic Python Concepts - Likely Non Interview questions Flashcards

1
Q

What are the ASCII ordinal values for 0, a, 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you accept user input?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you remove spaces from a string?

A

Use the replace method: string.replace(‘ ‘,’’)

string = string.replace(‘ ‘,’’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an easy way to create a set of alphabetical characters?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How could you print words in a sentence that starts with a specific letter?

A
  • 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How could you retrieve all unique characters from a string?

A

Use the set method: set(string)

string = set(string)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How could you use list comprehension to create a list of numbers between 1 and 50 that are divisible by 3?

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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”.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Fibonnaci Sequence: Write a program that prints the Fibonacci Sequence up to the given value.

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you provide default values into a function?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you use a dictionary as counter variables and increment them as needed?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How could you reverse a string?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How could you Print or Return one of two options depending on boolean value?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly