Quick Review of Basic Concepts in Python
----------------------------------------------------------------------------------------------------------------------
1) Variables
A variable in Python is a symbolic name that is a reference or pointer to an object. Once an
object is assigned to a variable, you can refer to the object by that name. Variables do not
require explicit declaration.
Example:
x=5 # integer
name = 'Alice' # string variable
2) Data Types
Python has several built-in data types for different kinds of values:
- int: Integer numbers (e.g., 10, -5)
- float: Floating-point numbers (e.g., 3.14, -2.7)
- str: Strings (e.g., 'hello', 'Python')
- bool: Boolean values (True or False)
- NoneType: Represents the absence of a value (None)
3) Collections
Python provides several collection data types to store multiple values in a single variable:
- List: An ordered, mutable collection of items. [1, 2, 3]
- Tuple: An ordered, immutable collection of items. (1, 2, 3)
- Dictionary: A collection of key-value pairs. {'name': 'Alice', 'age': 25}
4) Function Definition
Functions in Python are defined using the def keyword. They help organize code into
reusable blocks.
Example 1:
def greet(name):
print(f"Hello, {name}!")
Call: greet('Alice')
Example 2:
def add(a, b):
return a + b
Call: result = add(1, 2)
5) if-else Conditionals
Conditional statements are used to execute code based on certain conditions.
Example:
x = 10
"
if x > 0:
print('Positive number')
else:
print('Non-positive number')
6) for loops + while loops
- for loop: Used to iterate over a sequence (like a list or string).
Example:
for i in range(5):
print(i)
- while loop: Repeats as long as a condition is true.
Example:
count = 0
while count < 5:
print(count)
count += 1
7) Module Importing and Usage
Modules are external Python files or libraries that can be imported and used within your
program. The import keyword is used to include them.
Example with the random module:
import random
print([Link](1, 10)) # Generates a random number between 1 and 10.
----------------------------------------------------------------------------------------------------------------------