Python Resources
Understanding Python Arguments Understanding Default Parameters in Python Understanding Python Functions Python While Loops Python Ternary Operator Introduction to If-Else Statements Python Comparison Operators Python If Statement Python Type Conversion Python Comments Python Constants Python Boolean Python Numbers Python Strings Understanding Python Variables Python IntroductionPython is a versatile language used in web development, data science, automation, and more.Â
Let's explore one of its core concepts: variables.
What Are Python Variables?
Think of a variable as a box. Just as you can store different items in a box, you can store various data in a Python variable.Â
Variables hold information that you might need to use or change later.
Why Use Variables?
Variables make coding efficient.Â
By using variables, you can replace a long piece of data with a short name.Â
This simplifies your code and makes it more readable.Â
Instead of writing out a number or string multiple times, store it in a variable.
Naming Variables
In Python, variable names can consist of letters, numbers, and underscores, but they can't start with a number.Â
For example, my_variable1
is valid, but 1_variable
is not. Keep names descriptive to make your code intuitive.
Python Data Types: A Quick Overview
A variable's data type tells Python what kind of data is stored. Let’s look at some common types:
- Integers are whole numbers, like
5
or-3
. - Floats are numbers with decimals, like
3.14
or-0.001
. - Strings are text, enclosed in quotes, like
"Hello, World!"
. - Booleans are
True
orFalse
, often used in conditions.
Python Numbers: Integers and Floats
Python distinguishes between whole and decimal numbers. Use integers for whole numbers and floats for decimals. Here's a quick example:
age = 23 # Integer
price = 19.99 # Float
Python Casting
Casting means converting one data type to another. You can turn an integer into a string, or a float into an integer. Here's how:
num = 5
str_num = str(num) # Now 'num' is a string "5"
pi = 3.14
whole_number = int(pi) # 'whole_number' is 3
Strings: Beyond Simple Text
Strings can include letters, numbers, and special characters. Use single, double, or triple quotes to define strings, like:
greeting = "Hello, World!"
paragraph = """This is a
multi-line string."""
Working with Booleans
Booleans reflect the truth of a statement, making them perfect for conditions. Is the number greater than ten? True
or False
can answer that.
is_sunny = True
print(is_sunny) # Outputs: True
Using Python Operators
Operators in Python perform operations on variables. There are arithmetic operators like +
, -
, *
, /
, comparison operators like ==
, !=
, >
, <
, and logical operators such as and
, or
, not
.
Storing Multiple Values: Python Lists, Tuples, Sets, and Dictionaries
Python offers several ways to store collections of data.
Python Lists
Lists are ordered and changeable, perfect for storing multiple related items. Access them using an index, starting at zero.
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Outputs: apple
Python Tuples
Tuples are like lists but can't be changed. They ensure data remains constant.
coordinates = (10.0, 20.0)
Python Sets
Sets are collections of unique items, great for removing duplicates.
unique_numbers = {1, 2, 3, 4}
Python Dictionaries
Dictionaries hold data as key-value pairs, excellent for organizing related information.
student = {"name": "John", "age": 21}
Making Decisions: Python If...Else
Control the flow of your program with conditions. If a condition is true, execute one block of code; otherwise, run another.
grade = 85
if grade >= 90:
print("A")
else:
print("Not an A")
Looping with Python: While and For Loops
Loops let you execute code repeatedly.
Python While Loops
Run code as long as a condition is true.
count = 0
while count < 5:
print(count)
count += 1
Python For Loops
Iterate over items in a sequence, like a list.
for fruit in fruits:
print(fruit)
Creating and Using Functions
Functions bundle code into reusable blocks, making programs more organized.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Python Lambda
Lambda functions are short, anonymous functions useful for simple operations.
square = lambda x: x * x
print(square(4)) # Outputs: 16
Python Arrays
Though Python doesn't have built-in arrays, lists can serve a similar purpose.
Exploring Python Classes and Objects
Object-oriented programming in Python lets you define classes as blueprints for objects.
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
my_car = Car("Toyota", "Camry")