Python Basics 1
Basics
Starting with the very basics. Python interpreter can work as a calculator. See the code chunk below, but first a note.
In all the code shown below, the output of running that piece of code chunk is shown immediately after the code chunk. The code can be edited for trying different cases.
Line 1 outputs the result of the input 1 + 3. Just typing 1 + 3 in the Python interpreter should also give the same result.
The second line just prints a string as is. Note that the print function can take both a numeric or string (and other variable types also) as the input (more on that later).
Variables
Similar to other programming languages, we can assign values to variables and use them later. Variables allow values to be assigned to memory and used, changed and retrieved later.
Whitespace
Whitespaces are very important in python. Instead of curly braces which are often used in other programming languages, a tab is used to indent code within a block in python. In my opinion, it is a little confusing to some one learning python after learning other languages, but something they could get used to after some exposure to the language. See the code block below, the statement on line 2 is within the if block, but the line 4 is outside the if block.
Python Variables: A Deep Dive into Different Types
In Python, variables are like containers that hold information. You use them to store data and perform operations on it within your programs.
Let’s dive into the types of variables you can create in Python:
1. Basic Data Types:
- Integers (int): These store whole numbers (positive or negative).
- Example:
age = 25 number = -10
- Example:
- Floating-point Numbers (float): Represent decimal numbers.
- Example:
price = 19.99
- Example:
- Strings (str): Used to store text characters.
- Example:
message = "Hello, world!"
- Example:
2. Boolean:
- Booleans (bool): These represent truth values (
TrueorFalse).- Example:
is_active = True
- Example:
3. Custom Data Structures (Lists, Tuples, Dictionaries):
Python provides powerful data structures for handling collections of information. Let’s explore some examples:
- List: A flexible container that can hold multiple items of different types.
- Example:
colors = ["red", "blue", "green"] - You can add, remove, or modify elements:
colors.append("yellow")
- Example:
- Tuple: An immutable collection of items (cannot be changed after creation).
- Example:
coordinates = (10, 20)
- Example:
- Dictionary: A collection of key-value pairs.
- Example:
person = {"name": "Alice", "age": 30} - You can access values using keys:
person["name"]
- Example:
4. Understanding the Importance of Data Types
Choosing the right variable type is crucial for efficient and correct program execution:
- Data Type Efficiency: Different data types have different memory requirements, influencing your code’s speed and efficiency.
- Type Inference: Python can often automatically deduce the type based on the assigned value (e.g.,
10is an integer). You don’t always need to explicitly specify the type.
5. Defining Variables: A Quick Recap
To create a variable, you use the assignment operator (=):
6. The Power of Dynamic Typing: Flexibility
Python’s dynamic typing is a significant advantage. You don’t need to declare the type explicitly (like in Java), making coding more flexible and faster.
Ready to Explore More?
There’s so much to discover about variables in Python! Check out these resources for further learning: * Python Documentation * W3Schools Python tutorial
Using the numpy package in Python.