Getting Started with Python: The Basics
Printing
The print() function is your way of communicating with the world. It displays whatever you put inside the parentheses on the screen.
Basic Data Types: String, Integer, and Float
Think of data types as the different "species" of information Python can handle.
| Data Type | Name | Example | Description |
|---|---|---|---|
| String | str |
"Nucleaus" |
Text wrapped in quotes (single or double). |
| Integer | int |
776 |
Whole numbers, positive or negative. |
| Float | float |
3.14 |
Numbers with a decimal point. |
name = "NMR Meets Biology" # String
version = 6 # Integer
completed = 0.95 # Float
print(type(name)) # This tells you the type of the variable
Operations on Strings, Integers, and Floats
In Python, the way operators behave depends entirely on the data type you are using. Applying an operator to the wrong type can either result in an error or a surprising result.
1. Integer & Float Operations
Both int and float support standard mathematical operators. When you perform an operation between an integer and a float, Python automatically converts the result to a float.
| Operator | Description | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 2.0 |
7.0 |
- |
Subtraction | 10 - 3 |
7 |
* |
Multiplication | 4 * 3 |
12 |
/ |
Division (always returns float) | 10 / 2 |
5.0 |
// |
Floor Division (removes decimal) | 10 // 3 |
3 |
% |
Modulo (remainder) | 10 % 3 |
1 |
** |
Exponent (Power) | 2 ** 3 |
8 |
2. String Operations
Strings can't do math in the traditional sense, but Python allows "concatenation" and "repetition" using math symbols.
-
Concatenation (
+): Combines two strings together. -
Repetition (
*): Repeats a string a specific number of times.
3. Type Conversion (Casting)
You cannot "add" a string and a number directly (e.g., "Age: " + 25 will crash). You must convert them first.
age = 25
# Convert int to str to print it with text
print("I am " + str(age) + " years old.")
# Convert str to int to do math
price_str = "100"
total = int(price_str) + 50 # Output: 150
Pro Tip: When you want to combine variables and strings easily, use f-strings. Just put an
fbefore the quotes and variables in{}:print(f"The total is {total}")
Collections (Lists & Dictionaries)
Sometimes you need to store more than just one piece of data. This is where Lists and Dictionaries come in.
A. Lists
A List is an ordered collection of items. It’s like a shopping list where every item has a specific position (index).
- Note: Python starts counting at 0.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
fruits.append("orange") # Adds to the end
B. Dictionaries
A Dictionary stores data in key-value pairs. It’s like a real-life dictionary: you look up a "word" (the key) to find its "definition" (the value).
user = {
"name": "Alex",
"age": 25,
"is_student": True
}
# Accessing a value using its key
print(user["name"]) # Output: Alex
You can iterate over all items inside dictionary:
For Loop
A for loop is used to iterate over a sequence (like a list or a dictionary). It tells Python to "do this action for every item in this collection."
Functions
Functions are blocks of code that only run when called. They allow you to write logic once and reuse it multiple times, preventing "spaghetti code."