Python Essentials: From First Commands to Custom Functions - Part 1
Starting your journey in Python can feel like learning a new language—because it is! Python acts as both a language and an interpreter, translating your human-readable code into the 0s and 1s that a computer understands.
When you run a command like $ python hello.py, the interpreter reads your file from top to bottom and left to right, executing your instructions step-by-step.
1. The Developer's Workspace: Terminal & Pseudocode
Before writing complex logic, you need to know how to interact with your machine.
- The Terminal: Your Command Line Interface (CLI) to the system.
- Creating a File: Simply type
code hello.pyin your terminal to start a new script. - Pseudocode: A method of planning your logic using comments. By writing out the steps in plain English first, you make the actual coding process much smoother.
# Ask user for their name
# Say hello to the user
name = input("What's your name? ")
print(f"hello, {name}")
2. Deep Dive: The print() Function
According to official documentation, the print function looks like this:
print(*objects, sep=' ', end='\n', file=None, flush=False)
- *objects: Indicates that you can pass multiple arguments to be printed.
- sep: The separator. By default, it's a space, but you can override it (e.g.,
sep="|"). - end: Determines what happens at the end of the line. By default, it's a newline (
\n).
3. Data Types & Type Conversion
Computers see everything you type on a keyboard as Strings (text). If you want to perform math, you must use Type Conversion.
| Data Type | Description | Example |
|---|---|---|
| Int | Integers (whole numbers) from negative to positive infinity. | age = int("25") |
| Float | Numbers with decimal points. | price = 19.99 |
| String | Text data. | name = "Ali" |
4. Masterful String Formatting
Python's f-strings (Format Strings) are a powerful way to include variables and format data inside a string. Using the {variable:specifier} syntax allows for precise control:
| Specifier | Meaning | Output Example |
|---|---|---|
.2f | 2 decimal places | 12.35 |
:, | Thousands separator | 1,000,000 |
:05d | Zero padded (width 5) | 00042 |
:^10 | Center align | Ali |
5. Clean Code with Methods & Functions
To keep your data tidy, Python offers built-in string methods like .strip() to remove whitespace or .title() to capitalize every word.
User-Defined Functions
You aren't limited to built-in functions. You can create your own using the def keyword.
def hello(to="world"):
print("hello,", to)
# If no argument is passed, it defaults to "world"
hello()
Note on Scope: A variable only exists within the context (the "scope") where it is defined. A variable created inside a function cannot be accessed outside of it.
6. Error Handling: Try vs. Except
Bugs are inevitable. Using try/except blocks allows your program to handle mistakes gracefully instead of crashing.
- Use it for: Converting user input (preventing
ValueError), opening missing files (FileNotFoundError), or preventing division by zero. - Don't use it for: Normal logic. If you can check a condition with an
ifstatement (likeif age > 18), do that instead!
try:
age = int(input("Enter age: "))
except ValueError:
print("That's not a valid number!")
Conclusion
Mastering these fundamentals—from understanding the interpreter to handling errors—is the first step toward building robust AI automations or web applications.
Next in the Series
👉 Python Essentials: From First Commands to Custom Functions - Part 2 — Dive into conditionals, Pythonic expressions, the match statement, and professional coding standards.
Share this post