Master Python Basics in 30 Minutes: Variables, Loops, Functions & More
Within just 30 minutes, this guide walks beginners through Python’s core syntax—including variables, data types, operators, control flow, functions, and essential data structures—providing clear explanations and runnable code examples so you can write your first programs and build a solid foundation.
This quick‑start guide helps beginners master Python’s essential syntax in about half an hour.
1. Why Choose Python?
Python is concise, readable, and powerful, widely used for automation, data analysis, web scraping, web development, and AI/ML. Its natural‑language‑like syntax makes it ideal for newcomers.
2. First Python Program: Hello World!
print("Hello, World!")The print() function outputs text to the console, a common debugging tool.
3. Variables and Basic Data Types
Variables are created without explicit type declarations:
name = "Alice" # str
age = 25 # int
height = 1.68 # float
is_student = True # boolPython is dynamically typed; use type() to inspect a variable’s type:
print(type(name))
print(type(age))4. Operators and Expressions
Arithmetic operators:
+ addition 3 + 2 = 5
- subtraction 5 - 2 = 3
* multiplication 3 * 4 = 12
/ division (float) 10 / 3 ≈ 3.333
// integer division 10 // 3 = 3
% modulus 10 % 3 = 1
** exponent 2 ** 3 = 8Comparison operators:
== equal 3 == 3 → True
!= not equal 3 != 4 → True
> greater 5 > 3 → True
< less 2 < 4 → True
>= greater or equal 5 >= 5 → True
<= less or equal 3 <= 5 → TrueLogical operators:
and True and False → False
or True or False → True
not not True → False5. Control Flow: Conditionals and Loops
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
else:
print("C") fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit) count = 0
while count < 5:
print(count)
count += 1Use break, continue, and pass to control loop execution.
6. Common Data Structures
List (mutable):
nums = [1, 2, 3]
nums.append(4)
nums[0] = 10Tuple (immutable):
point = (3, 5)
x, y = pointDictionary (key‑value pairs):
person = {"name": "Alice", "age": 25}
person["email"] = "[email protected]"
del person["age"]Set (unique elements):
unique_nums = {1, 2, 3, 2, 1} # becomes {1, 2, 3}7. Functions
def greet(name):
print("Hello, " + name)
greet("Bob") def add(a, b):
return a + b
result = add(3, 5)
print(result) # 8Functions can have default parameters, *args, and **kwargs for flexible arguments.
8. Modules
import math
print(math.sqrt(16)) # 4.0 from datetime import datetime
print(datetime.now()) import pandas as pd9. Practice: Simple Calculator
def add(a, b): return a + b
def subtract(a, b): return a - b
def multiply(a, b): return a * b
def divide(a, b): return a / b if b != 0 else "cannot divide by 0"
# Input handling and operation selection omitted for brevity10. Summary Table
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
