Python Basics

Gathering Insight
3 min readJan 24, 2025

--

A Comprehensive Beginner’s Guide

Why Python?

Python has become one of the most popular programming languages worldwide, and it’s not hard to see why. Whether you’re diving into data analysis, artificial intelligence, or building servers and backends, Python offers incredible flexibility and ease of use.

However, it’s worth noting that Python is comparatively slower than some other languages and isn’t typically used for app development or user interface design. Additionally, it gives less control over hardware-level operations. Despite these limitations, Python remains an excellent starting point for beginners due to its simplicity and the transferable knowledge it provides for learning other languages like C++ or JavaScript.

Setting Up for Success

Before you begin coding, you’ll need a code editor. Here are some great options:

  • VS Code (recommended)
  • Sublime Text
  • PyCharm
  • Atom

Each editor has its strengths, so explore and find what works best for you.

Python Basics

How Python Executes Code

Python code runs sequentially, line by line, from top to bottom. Keep this in mind when writing scripts.

Handy Editor Shortcuts

  • Commenting: Highlight code and press Ctrl + / to comment it out.
  • Duplicate Lines: Use Alt + Shift + Down Arrow to copy and paste a line below.

Variable Naming Rules

Mandatory:

  • Only use letters (A-Z, a-z) and numbers (0–9).
  • Avoid starting with a number (e.g., 1variable is invalid).
  • Python is case-sensitive, so variable and Variable are different.
  • Don’t use built-in function names (e.g., avoid naming variables print).

Beneficial:

  • Be clear and descriptive (e.g., user_name instead of u).
  • Use snake_case for readability.

Methods and Functions

Functions are reusable blocks of code that perform specific tasks. For example:

  • Input and Output:
  • input() lets you receive user input.
  • print() displays information.
  • Integer Conversion: The int() function converts strings to integers.

Data Containers

Python provides several ways to organize data:

  • Lists ([]): Mutable (modifiable) containers.
  • Tuples (()): Immutable (cannot be changed).
  • Sets ({}): Collections with unique entries.
  • Dictionaries ({key: value}): Perfect for organizing data in key-value pairs.

Pro Tip: Use the set() function to quickly find duplicates in data.

Indexing and Slicing

Python allows you to access individual elements or subsets of data containers using indexing and slicing. For example:

my_list = [10, 20, 30, 40]
print(my_list[1]) # Outputs 20
print(my_list[:2]) # Outputs [10, 20]

Control Flow

Control flow helps determine the logic of your program.

  • If Statements: Execute code based on conditions.

Loops:

  • While Loops: Repeat as long as a condition is true.
  • For Loops: Iterate over each item in a container.

Python uses Boolean values (True or False) to manage control flow. Remember:

  • Empty containers, strings, or 0 are considered False.
  • Everything else is Truthy (treated as True).

Advanced Tools

  • F-Strings: Simplify string formatting:
name = "John"
print(f"Hello, {name}!")

List Comprehensions: Create lists concisely:

squares = [x**2 for x in range(5)]
print(squares) # Outputs [0, 1, 4, 9, 16]

Classes and Object-Oriented Programming

Classes help you organize your code by grouping variables (attributes) and functions (methods).

  • Self: Refers to the instance of the class and must be the first parameter in all methods.
  • Dunder Methods: Special methods like __init__ (constructor) allow custom behavior for your class.
  • Inheritance: Classes can inherit methods and attributes from other classes, enabling code reuse.

Example:

class Animal:
def __init__(self, name):
self.name = name
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
print(f"{self.name} makes a sound.")

class Dog(Animal):
def speak(self):
print(f"{self.name} barks.")

dog = Dog("Rex")
dog.speak() # Outputs: Rex barks.

Modules

Modules provide additional functionality and help organize your code. Python includes both standard and external modules.

Standard Library Examples:

  • math for mathematical operations (e.g., square root, sine, cosine).
  • random for generating random numbers.
  • string for working with text.

External Modules: Install using pip in your terminal. Popular modules include:

  • pygame for game development.
  • matplotlib for data visualization.
  • BeautifulSoup for web scraping.

Final Thoughts

Python is a versatile and beginner-friendly programming language. By understanding its basic building blocks — from variables and functions to classes and modules — you’re well on your way to becoming a proficient Python programmer. Happy coding!

--

--

Gathering Insight
Gathering Insight

Written by Gathering Insight

A place to leave my understandings and correlations from my notes.

No responses yet