Python Fundamentals
Python Variables and Data Types
14 Sep 2023🐍 Welcome to the first day of our Python tutorial series! Today, we’re diving into the fundamentals of Python, the versatile and powerful programming language.
Python Introduction: Python is known for its simplicity and readability, making it an excellent choice for beginners and experienced developers alike.
Why Python? Python’s versatility extends to data science, web development, machine learning, and more. Whether you’re a data scientist, developer, or just curious about programming, Python is a valuable skill to have.
In this tutorial, we’ll delve into the fundamental concepts of Python variables and data types. These are the building blocks of any Python program, and understanding them is crucial for writing effective code.
1. Variables in Python:
- Variables are used to store data in Python.
- Unlike some other programming languages, Python is dynamically typed, meaning you don’t need to declare a variable’s type explicitly.
- You can assign a value to a variable using the assignment operator
=
.
Example:
# Variable assignment
x = 10
name = "John"
2. Data Types:
- Python has several built-in data types. The most common ones include:
- int: Integer data type (e.g., 5, -3, 0).
- float: Floating-point data type (e.g., 3.14, -0.5).
- str: String data type (e.g., “Hello, Python!”).
- bool: Boolean data type (e.g., True, False).
- list: Ordered collection of items (e.g., [1, 2, 3]).
- tuple: Ordered, immutable collection (e.g., (1, 2, 3)).
- dict: Key-value pairs (e.g., {“name”: “Alice”, “age”: 30}).
3. Variable Naming Rules:
- Variable names must start with a letter (a-z, A-Z) or an underscore (_).
- Subsequent characters can be letters, underscores, or digits (0-9).
- Variable names are case-sensitive (e.g.,
myVar
andmyvar
are different).
4. Type Conversion:
- You can convert between data types using built-in functions like
int()
,float()
, andstr()
. - Type conversion is essential when performing operations involving different data types.
Example:
# Type conversion
x = 5
y = "10"
result = x + int(y) # Convert y to int and perform addition
5. Checking Data Types:
- You can check the data type of a variable using the
type()
function.
Example:
# Checking data type
x = 5
print(type(x)) # Output: <class 'int'>
Summary:
- Variables are used to store data in Python, and Python is dynamically typed.
- Python has various data types, including int, float, str, bool, list, tuple, and dict.
- Follow naming rules for variables and use type conversion when needed.
In the next tutorial, we’ll explore control flow in Python, including if statements and loops. Stay tuned for more Python fundamentals!
Let’s embark on this Python journey together! 🚀