1/20
Generated by Clajude
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
How do you create variables and check their types?
name = 'John'
age = 25
print(type(name), type(age))
>>> <class 'str'> <class 'int'>
How do you create and manipulate lists?
fruits=['apple','banana']
fruits.append('orange')
fruits.extend(['grape','kiwi'])
print(fruits[0]) # First element
print(fruits[-1]) # Last element
>>>apple
>>>kiwi
How do you create and access dictionary data?
student = {'name': 'Alice', 'age': 22}
print(student['name'])
student['grade'] = 'A'
print(student.get('city', 'Unknown'))
>>>Alice
>>>Unknown
How do you iterate through data with for loops?
# Iterate over a list
for item in ['a', 'b', 'c']:
print(item)
>>>a
>>>b
>>>c
# Iterate with index
for i in range(5):
print(f"Number: {i}")
>>>Number: 0
>>>Number: 1
>>>Number: 2
>>>Number: 3
>>>Number: 4
How do you create lists efficiently using list comprehensions?
# Basic list comprehension
squares = [x**2 for x in range(5)]
print(squares)
>>>[0, 1, 4, 9, 16]
# With condition
evens = [x for x in range(10) if x % 2 == 0]
print(evens)
>>>[0, 2, 4, 6, 8]
# Nested comprehension
matrix = [[i*j for j in range(3)] for i in range(3)]
print(matrix)
[[0, 0, 0], [0, 1, 2], [0, 2, 4]]
How do you define and call functions?
def greet(name, greeting='Hello'):
return f'{greeting}, {name}!'
# Function calls
result = greet('Alice')
result2 = greet('Bob', 'Hi') # Specified a greeting other than the default 'Hello'
print(result)
>>>Hello, Alice!
print(result2)
>>>Hi, Bob!
How do you handle conditional statements?
x = 10
if x > 0:
print('positive')
elif x < 0:
print('negative')
else:
print('zero')
# Ternary operator
result = 'positive' if x > 0 else 'not positive'
>>>positive
How do you work with tuples?
# Create tuple
coords = (3, 5)
point = 1, 2, 3 # Parentheses optional
# Unpack tuple
x,y=coords
print(coords[0]) # Access by index
>>>3
# coords[0] = 10 # Error: tuples are immutable
How do you work with sets?
# Create set
unique_nums = {1, 2, 3, 3, 4} # Duplicates removed
my_set = set([1, 2, 3])
# Set operations
my_set.add(5)
print(my_set)
>>>{1, 2, 3, 5}
my_set.remove(1)
print(1 in my_set)
>>>False
print(my_set)
>>>{2, 3, 5}
How do you perform string operations?
text = "Hello World'
print(text.lower())
>>>hello world
print(text.upper())
>>>HELLO WORLD
print(text.split())
>>>['Hello', 'World']
print(text.replace('World', 'Python'))
>>>Hello Python
print(text.startswith('Hello'))
>>>True
How do you use while loops?
count = 0
while count < 3:
print(f'Count is: {count}')
count += 1
>>> Count is: 0
>>> Count is: 1
>>> Count is: 2
# With break and continue
How do you work with range()?
# Basic range
for i in range(5): # 0, 1, 2, 3, 4
print(i)
>>> 0
>>> 1
>>> 2
>>> 3
>>> 4
# Range with start, stop, step
for i in range(0, 10, 2):
print(i)
>>> 0
>>> 2
>>> 4
>>> 6
>>> 8
# Convert to list
numbers = list(range(5))
print(numbers)
>>> [0, 1, 2, 3, 4]
How do you check if an item is in a list/dict?
fruits = ['apple', 'banana']
student = {'name': 'Alice', 'age': 22}
print('apple' in fruits)
>>> True
print('name' in student)
>>> True
print('grade' in student)
>>> False
How do you sort lists?
numbers = [3, 1, 4, 1, 5]
# In-place sorting
numbers.sort()
print(numbers)
>>> [1, 1, 3, 4, 5]
# Return new sorted list
sorted_nums = sorted([3, 1, 4], reverse=True)
print(sorted_nums)
>>> [4, 3, 1]
How do you remove items from lists?
fruits = ['apple', 'banana', 'orange']
# Remove by value
fruits.remove('apple')
print(fruits)
>>> ['banana', 'orange']
# Remove by index
removed = fruits.pop(0)
print(fruits)
>>> ['banana', 'orange']
# Delete by index
del fruits[0]
print(fruits)
>>> ['banana', 'orange']
How do you get user input?
name = input('Enter your name: ')
age = int(input('Enter your age: '))
height = float(input('Enter height: '))
print(f'Hello {name}, you are {age} years old')
# Upon running, a text box will pop up for each option and user will enter values. For example, Name = Oliver, age = 89
>>> Hello Oliver, you are 89 years old
How do you convert between data types?
# String conversions to another data type
num_str = str(42)
>>> 42
num_int = int('42')
>>> 42
num_float = float('3.14')
>>> 3.14
# Collection conversions
my_list = list('hello')
print(my_list)
>>> ['h', 'e', 'l', 'l', 'o']
my_tuple=tuple([1, 2, 3])
>>> (1, 2, 3)
How do you work with multiple assignment?
# Multiple assignment
a, b = 1, 2
x = y = z = 0
# Swapping variables
a, b = b, a
# Unpacking
coords = (3, 5)
x, y = coords
print(coords)
>>> (3, 5)
How do you use enumerate()?
fruits = ['apple', 'banana', 'cherry']
# Get index and value
for i, fruit in enumerate(fruits):
print(f'{i}: {fruit}')
>>> 0: apple
>>> 1: banana
>>> 2: cherry
# Start counting from 1
for i, fruit in enumerate(fruits, 1):
print(f'{i}: {fruit}')
>>> 1: apple
>>> 2: banana
>>> 3: cherry
How do you use zip()?
names = [‘Alice’, ‘Bob’, ‘Charlie’]
ages = [25, 30, 35]
cities = [‘NYC’, ‘LA’, ‘Chicago’]
# Combine multiple lists
for name, age, city in zip(names, ages, cities):
print(f'{name}, {age}, {city}')
>>> Alice, 25, NYC
>>> Bob, 30, LA
>>> Charlie, 35, Chicago
How do you create NumPy arrays?
import numpy as np
# From list
arr = np.array([1, 2, 3])
print(arr)
>>> [1 2 3]
# Create special arrays
zeros = np.zeros((3, 4)) # 3x4 array of zeros
>>> [[0. 0. 0. 0.]
>>> [0. 0. 0. 0.]
>>> [0. 0. 0. 0.]]
ones = np.ones((2, 3)) # 2x3 array of ones
>>> [[1. 1. 1.]
>>> [1. 1. 1.]]
eye = np.eye(3) # 3x3 identity matrix
>>> [[1. 0. 0.]
>>> [0. 1. 0.]
>>> [0. 0. 1.]]