What is Python?
Python, a high-level, interpreted programming language, has emerged as one of the most popular and versatile languages in the modern tech world. Known for its simplicity and readability. This language supports multiple programming paradigms, including procedural, object-oriented, and functional programming.
Python is very importance. It’s extensively used in various domains such as web development, data analysis, artificial intelligence, scientific computing, and more. Its vast collection of libraries and frameworks, like Django for web development and Pandas for data analysis, makes it a go-to choice for developers and researchers alike. Python’s strong community support and ongoing development ensure its relevance in the fast-evolving tech landscape.
1. Variables in Python
Variables in Python are like containers for storing data values. Unlike some languages, Python does not require explicit declaration to reserve memory space. The variable is created the moment you first assign a value to it. Variables in Python can hold different data types, and their type can change, reflecting Python’s dynamic nature.
x = 10
y = "Hello, Python!"
print(x) # output is 10
print(y) # output is Hello, Pyhon!
2. Numbers
Python supports various numeric data types including integers, float (decimal numbers), and complex numbers. These data types are immutable and provide a flexible way to work with numerical data in different contexts, from simple arithmetic to complex mathematical operations.
# the is an Integer
a = 5
# Float
b = 3.2
# Complex
c = 1j or c = 5+2j
print(a, b, c)
3. Strings
Strings in Python are arrays of bytes representing Unicode characters. Python offers a wide range of built-in methods for manipulating strings., making it easy to perform operations like concatenation, slicing, and formatting.
str1 = "Hello"
str2 = "Python"
# Concatenation
combined_str = str1 + " " + str2
# Slicing
sub_str = str1[1:4]
print(combined_str) # Outputs: Hello Python
print(sub_str) # Outputs: ell
# We can do a lot of other fun things.
4. Lists
Lists are one of the most versatile data structures in Python. They can contain items of different data types and are mutable, meaning their content can be changed after they are created. Lists support various methods for common tasks like adding, removing, and sorting items.
# This is my favorite.
list1 = [1, 2, 3, "Python", "List"]
list1.append(4) # Add an element
list1.remove("List") # Remove an element
print(list1) # Outputs: [1, 2, 3, 'Python', 4]
# Don't forget to practice more and more because practice makes permanent.
5. Dictionaries
Dictionaries in Python are unordered collections of data in a key:value pair form. They are optimized for retrieving data and are essential for efficient data manipulation and storage. Dictionaries are mutable and indexed by keys, which are unique within a dictionary.
my_dict = {"name": "Alice", "age": 25}
my_dict["email"] = "alice@example.com" # Adding a new key-value pair
print(my_dict) # Outputs: {'name': 'Alice', 'age': 25, 'email': 'alice@example.com'}
6. Sets
Sets are unordered collections of unique elements in Python. They are used to perform mathematical set operations like union, intersection, difference, and symmetric difference. Sets are mutable and provide an efficient way to remove duplicate values from a collection.
my_set = {1, 2, 3, 4, 4, 5}
my_set.add(6) # Adding an element
print(my_set) # Outputs: {1, 2, 3, 4, 5, 6}
7. Tuples
Tuples are ordered collections of items, similar to lists. However, tuples are immutable, which means once a tuple is created, its values cannot be changed. Tuples are used for data that shouldn’t be modified and can be more memory efficient than lists.
# We use tuples when we are certain that we will not change it in the future.
my_tuple = (1, 2, 3, "Python")
print(my_tuple) # Outputs: (1, 2, 3, 'Python')
8. Control Structures
Control structures in Python allow for decision making and controlling the flow of execution.
If conditions: These are used to execute a block of code if a specified condition is true. Python supports the usual logical conditions from mathematics.
x = 10
if x > 5:
print("x is greater than 5")
For loops: For loops in Python are used for iterating over a sequence (like a list, tuple, dictionary, set, or string), executing a block of code multiple times.
# Be careful when using for loops; the infinite loop is a monster.
for i in range(5):
print(i)
Conclusion
This is just the fundamentals of Python. Practice more and do some projects to reinforce this knowledge. In the second part of this series, we will delve into advanced Python. Stay tuned and be ready for the next part.
If you want to see the whole roadmap to become a data scientist, click here.