Welcome to the world of Python programming! Whether you’re completely new to coding or just getting back in the game, this post will gently walk you through the basics of Python—no prior experience needed. 😊
We’ll cover:
- Printing your first message
- Creating and using variables
- Key facts about variables
- Python’s basic data types
- Arithmetic operations
- String tricks and tips
Let’s dive right in! 🚀
🖨️ The print() Function
Your very first line of Python code is likely going to use the print() function. It simply displays whatever you ask it to.
print("Hello, world!")
Output:
Hello, world!
You can also print numbers or math expressions:
print(5 + 3)
📦 Creating Variables in Python
Variables are like containers for storing data. You give a variable a name, and assign a value to it.
name = "Alice"
age = 30
🔁 Assignment and Using Variables
Variables aren’t static—you can reuse and change them:
greeting = "Hello"
print(greeting, name) # Outputs: Hello Alice
age = 31 # Alice just got a year older!
🧠 Key Things to Know About Variables
- ✅ Variable names are case-sensitive (Age and age are different).
- ✅ Must start with a letter or underscore (_), not a number.
- ✅ Can include letters, numbers, and underscores.
- ✅ Use clear, descriptive names like user_score or total_amount.
🔢 Basic Data Types in Python
Python comes with several built-in data types:
Type | Example | Description |
str | “Hello” | String (text) |
int | 10, -3 | Integer (whole numbers) |
float | 3.14, 0.5 | Decimal numbers |
bool | True, False | Boolean (yes/no) |
Example:
title = "Engineer"
salary = 50000
rating = 4.8
is_active = True
➕ Arithmetic Operations
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a // b) # 3 (floor division)
print(a % b) # 1 (remainder)
print(a ** b) # 1000 (exponentiation)
🔤 String Operations
first = "Good"
second = "Morning"
message = first + " " + second
print(message) # Good Morning
✨ Repetition
print("Hi! " * 3) # Hi! Hi! Hi!
🔢 String Length
text = "Python"
print(len(text)) # 6
🧩 Indexing Strings
word = "Python"
print(word[0]) # P
print(word[-1]) # n
🔪 String Slicing
Slicing lets you extract a part of a string by specifying a start and end index using this format:
string[start:end]
📌 The result includes characters from start
up to but not including end
.
Let’s see it in action:
my_string = "Python"
# Slice from index 0 to 2 (not including 2)
print(my_string[0:2]) # Output: Py
# Slice from index 2 to 5
print(my_string[2:5]) # Output: tho
# Slice from start to index 3
print(my_string[:3]) # Output: Pyt
# Slice from index 3 to end
print(my_string[3:]) # Output: hon
# Slice the whole string
print(my_string[:]) # Output: Python
❗ Important Note:
Strings are immutable, meaning you cannot change a character by assigning a new value to an index.
my_string = "Python"
# my_string[0] = "J" # ❌ This will raise a TypeError!
If you want to change part of a string, you need to create a new one—using methods like replace()
, slicing, or concatenation.
🛠️ Useful String Methods
Strings in Python come with a variety of built-in methods that let you manipulate text easily. You use them with dot notation, like this:
msg = "hello world"
print(msg.upper()) # HELLO WORLD
print(msg.capitalize()) # Hello world
print(msg.replace("world", "Python")) # hello Python
🔼 .upper()
and .lower()
Convert all characters to uppercase or lowercase.
print(text.upper()) # HELLO PYTHON WORLD!
print(text.lower()) # hello python world!
📏 String Length: len()
Just like with lists, you can use the len()
function to find out how many characters are in a string (including spaces and punctuation!).
greeting = "Hello, World!"
length = len(greeting)
print(f"The string '{greeting}' has {length} characters.")
# Output: The string 'Hello, World!' has 13 characters.
empty_string = ""
print(len(empty_string)) # Output: 0
🔹 Tip: The f"..."
format above is called an f-string. It’s a powerful and clean way to embed variables directly inside strings!
✂️ .strip()
Removes whitespace from the beginning and end.
stripped = text.strip()
print(stripped) # Hello Python World!
🔄 .replace(old, new)
Replaces a part of the string with something else.
replaced = stripped.replace("Python", "AI")
print(replaced) # Hello AI World!
🪓 .split(separator)
Splits a string into a list based on a separator.
words = stripped.split(" ")
print(words) # ['Hello', 'Python', 'World!']
sentence = "first,second,third"
items = sentence.split(",")
print(items) # ['first', 'second', 'third']
🔸 Remember: These methods don’t modify the original string—they return a new string instead, since strings are immutable.
🎯 In a Nutshell
Let’s recap:
- ✅ print() shows output on the screen
- ✅ Variables store your data
- ✅ Python understands numbers, text, and booleans
- ✅ You can perform math and text operations easily
Python is simple to learn but powerful enough to build amazing things. This is just the beginning of your journey—keep practicing, explore new things, and build something awesome.
🙌 What’s Next?
If you’re enjoying Python so far, try this small challenge:
# Try it yourself!
first_name = "Ada"
last_name = "Lovelace"
full_name = first_name + " " + last_name
print("Welcome,", full_name + "!")