Python Variables, Data Types, Control Structures, Functions, and Modules Overview
This article introduces Python fundamentals, covering variable declaration, built‑in data types, naming rules, control flow statements, function definitions with parameters, and module import techniques, providing code examples for each concept to aid beginners in programming.
Variables and Data Types In Python, variables store data without explicit type declaration. Example assignments:
x = 10
y = "Hello, world!"
z = TrueVariable naming rules: letters, digits, underscores; cannot start with a digit; case‑sensitive.
Data Types Python includes numeric types (int, float, complex), strings, booleans, sequence types (list, tuple), and mapping type (dict). Examples:
# numeric types
int: 10
float: 3.14
complex: 1+2j
# string
name = "Alice"
greeting = "Hello, " + name
print(greeting) # Hello, Alice
# boolean
is_student = True
is_teacher = False
# list
numbers = [1, 2, 3, 4]
names = ["Alice", "Bob", "Charlie"]
# tuple
coordinates = (10, 20, 30)
# dict
person = {"name": "Alice", "age": 25, "city": "New York"}Control Structures Conditional statements and loops control program flow. Example if‑else:
age = 20
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")Nested conditions and logical operators (and, or, not) are also demonstrated. For loops:
# iterate list
for name in names:
print(name)
# range
for i in range(5):
print(i)While loop example:
count = 0
while count < 5:
print(count)
count += 1Function Definition and Calls Functions encapsulate reusable code. Basic definition:
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Hello, AliceExamples of functions with parameters, default values, *args, and **kwargs are provided.
Module Import Importing modules or specific functions extends functionality. Examples:
import math
print(math.sqrt(16)) # 4.0
from math import sqrt
print(sqrt(16)) # 4.0
import math as m
print(m.sqrt(16)) # 4.0These foundational concepts are essential for further learning, such as writing automated test scripts.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.