Python Fundamentals: A Beginner's Guide to Getting Started


Introduction
Python is one of the most popular programming languages due to its simplicity, readability, and versatility. Whether you're a beginner or an experienced developer, mastering Python fundamentals is essential. In this blog, we'll cover core Python concepts, from installation to exception handling.
1. Installing Python and Setting Up Your IDE
Before diving into coding, you need to install Python and choose an Integrated Development Environment (IDE).
Installation
Download Python 3.12+ from the official Python website.
Follow the installation instructions for your operating system.
Recommended IDEs
VS Code (Lightweight, customizable, great for beginners)
PyCharm (Full-featured IDE for professional development)
2. Variables and Data Types
Python supports various data types:
# Integers and Floats
age = 25 # int
price = 19.99 # float
# Strings
name = "Alice" # str
# Booleans
is_active = True # bool
# None (represents absence of value)
result = None # NoneType
3. Input and Output Operations
Output (Printing)
print("Hello, World!")
print(f"Your name is {name} and you are {age} years old.") # f-strings (Python 3.6+)
Input (User Interaction)
user_name = input("Enter your name: ")
user_age = int(input("Enter your age: ")) # Convert input to integer
4. Operators in Python
Python supports different types of operators:
Arithmetic Operators
a = 10 + 5 # Addition
b = 10 * 2 # Multiplication
c = 10 ** 2 # Exponentiation (100)
Comparison Operators
x = 10 > 5 # True
y = 10 == 10 # True
Logical Operators
is_valid = True and False # False
is_logged_in = True or False # True
5. Conditional Statements (if, elif, else)
Control the flow of your program using conditions:
age = 18
if age < 13:
print("Child")
elif age < 20:
print("Teenager")
else:
print("Adult")
# Ternary Operator
status = "Adult" if age >= 18 else "Minor"
6. Loops (for and while)
For Loop
for i in range(5): # 0 to 4
print(i)
While Loop
count = 0
while count < 5:
print(count)
count += 1
Loop Control (break, continue)
for num in range(10):
if num == 3:
continue # Skip this iteration
if num == 7:
break # Exit loop
print(num)
7. Functions and Recursion
Basic Function
def greet(name):
return f"Hello, {name}!"
print(greet("Bob"))
Recursion (Factorial Example)
def factorial(n):
return 1 if n <= 1 else n * factorial(n - 1)
print(factorial(5)) # 120
8. Data Structures (Lists, Tuples, Sets, Dictionaries)
Lists (Mutable, Ordered)
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits[0]) # "apple"
Tuples (Immutable, Ordered)
coordinates = (10, 20)
# coordinates[0] = 5 # Error (Tuples are immutable)
Sets (Unique Elements, Unordered)
unique_numbers = {1, 2, 2, 3} # {1, 2, 3}
Dictionaries (Key-Value Pairs)
person = {"name": "Alice", "age": 25}
person["age"] = 26 # Update value
print(person["name"]) # "Alice"
9. String Manipulation
Python provides powerful string operations:
text = "Python Programming"
# Common String Methods
print(text.upper()) # "PYTHON PROGRAMMING"
print(text.lower()) # "python programming"
print(text.split()) # ['Python', 'Programming']
print(text.replace("P", "J")) # "Jython Jrogramming"
# String Formatting
name = "Bob"
print(f"Hello, {name}!") # f-strings (Modern Python)
print("Hello, {}!".format(name)) # Older method
10. Exception Handling (try, except, finally)
Prevent crashes by handling errors gracefully:
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
except Exception as e:
print(f"An error occurred: {e}")
else:
print("No errors occurred")
finally:
print("This always runs")
# Raising Custom Errors
def validate_age(age):
if age < 0:
raise ValueError("Age cannot be negative")
Subscribe to my newsletter
Read articles from Kolluru Pawan directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
