Python Fundamentals Flashcards
(81 cards)
How do you print ‘Hello, world!’ in Python?
print(“Hello, world!”)
How to create a date format with print()?
print(‘06’, ‘04’, ‘2003’, sep=’–’)
date – my birthday
How to ask a user to input their name?
input(“Enter your name: “)
What are two ways to write a multiline comments?
’#’To be
‘#’or not to be.
””” To be
or not to be.”””
How to make a variable with four different data types?
x = “Hello, world!”
x = 50 #int
x = 60.5 #float
x = 3j #complex
1 for text, 3 for numbers
How to check a data type?
By using type().
*print(type(x))
What are the conditional statements?
if–elif–else
How to use for loop in the range from 0 to 5?
for i in range(5):
print(i)
How to use while loop?
i = 0
while i < 5:
print(i)
i += 1
What are two loop control statements?
- break (exits the loop)
- continue (skips the current iteration)
How to use enumerate() to get index and value in a loop?
items=[]
for i, item in enumerate(items):
print (i, item)
list ‘items’
How to use Addition, Subtraction, Multiplication, and Division Assignment Operator?
a += b #a = a + b
a -= b
a *= b
a /= b
a = a + b
a = a - b
a = a * b
a = a / b
How to make a function?
def function():
function body
What is the function return statement?
The statement used to terminate a function and return to the function caller with the provided value or data item.
What are two ways to format a print() function?
print(“Result is {}”.format(variable))
or
print(f”Result is {variable}”)
Особливості {} в print()?
- {} - це місце, куди підставляється значення, яке передається в .format().
- Якщо {} кілька, .format() може приймати стільки ж аргументів.
- У {} можна використовувати індекси. (print(“{1} loves {0}”)
How to make a for loop with range() function?
for i in range(5):
print(i, end=” “)
Роль *args і **kwargs?
Дозволяють писати гнучкі функції, які можуть приймати змінну кількість аргументів.
*args - збирають дані у кортежі.
**kwargs - збирають дані у словники.
Як використовувати *args **kwargs для запису замовлень?
def order_summary (*items, **details):
print(“Замовлення містить: “, items)
print(“Деталі замовлення: “, details)
order_summary(“піца”, “сік”, адреса=”Київ, вул. Шевченка, 10”, час=”18:00”)
What is the syntax of walrus operator?
a := expression
What is the program for shortening a list (a = [1, 2, 3, 4, 5]) to three elements?
while (x := len(a)) > 2:
a.pop()
print(x)
What is the syntax of comparison operators?
- Greater than
- Less than
- Equal to
- Not equal to
- Grater than or equal to
- Less than or equal to
- x > y
- x < y
- x == y
- x != y
- x >= y
- x <= y
What is the syntax of chaining comparison operators?
a op1 b op2 c
What are logical operators used to?
To combine conditional statements (multiple conditions).