

Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!


This study guide is designed to help beginners understand the most essential Python programming concepts: variables, loops, and functions. These are the building blocks of most Python programs and are crucial for assignments, projects, and interviews.
Variables are used to store data in a program. Python uses dynamic typing, which means you don't need to declare the data type. Examples:
● x = 5 # integer ● name = 'Anna' # string ● pi = 3.14 # float
Loops let you repeat code. Python has two main types: for loops and while loops.
Example: for loop
for i in range(3): print(i) # Prints 0, 1, 2
Example: while loop
count = 0 while count < 3: print(count) count += 1
Functions allow you to reuse blocks of code. Use the 'def' keyword.
Example:
def greet(name): return 'Hello ' + name
print(greet('Anna')) # Output: Hello Anna
● 1) Create a variable named 'age' and set it to 21. ● Solution: age = 21 ● 2) Use a loop to print numbers 1 to 5. ● Solution: for i in range(1, 6): print(i) ● 3) Write a function that adds two numbers. ● Solution: def add(a, b): return a + b
● ❌ Using = instead of == in comparisons (e.g., if x = 5 ❌ → if x == 5 ✅) ● ❌ Forgetting indentation (Python uses whitespace to define blocks) ● ❌ Not returning a value from a function ● ❌ Using variables before assigning a value
● ✔ Use descriptive variable names (e.g., total_price instead of tp) ● ✔ Test your code frequently with print() ● ✔ Use comments (#) to explain sections of your code ● ✔ Practice solving problems on platforms like LeetCode or HackerRank