Friday, 18 October 2024

Python For Everyone

 

What is Python Programming Language?

Python is a high-level programming language known for its simplicity and readability. It was created by Guido Van Rossum and first released in 1991. Python is widely used in various fields such as web-development, data science, artificial intelligence, scientific computing, automation and more. Python comes with a comprehensive standard library that provides modules and packages for tasks ranging from file I/O network programming, making it convenient for various applications.


Who can excel in Python?

The qualities possess by the person who have the potential to excel in it are analytical thinking, curiosity, attention to detail, problem-solving skills, adaptability and passion for technology along with little bit of madness for being exceptional. These traits form a solid foundation for acquiring proficiency in Python Programming and thriving in the dynamic world of software development.

How to install python in your computer?

  • Download the latest python version for Windows from the official Python website https://www.python.org/.
  • Open the downloaded file from your file manager and double click on it to start the installation process, if prompted by the User Account Control dialog, click ‘Yes’ to allow the installer to make changes to your system
  • Now click on the user-friendly installation wizard and click on the ‘Install Now’ button to start the installation process.
  • Once the installation is complete, you can verify it by opening a command prompt and typing ‘python — version’. This should display the installed Python version.

Let’s start coding some Python.

1. Hello, World

In Python, you can start by printing “Hello World!” to the screen. Open up a text editor like VS Code or PyCharm and type:

print("Hello, World!")

Now save that file with some name to it and run it from the text editor. Then you can see Hello, World! printed on the screen.

2. Variables in Python

In Python, a variable is like a container that holds a value. You can think of it as a labeled box where you can store different types of information, such as numbers, text, or even more complex data structures. You need to follow the given guidelines while declaring the variables in python.

  • Variable names can contain letters, digits, and underscores but they cannot start with a digit.
  • Variables names are case-sensitive.
  • Use descriptive names for readability.
  • Use the ‘=’ operator to assign a value to a variable.
  • Python supports various data types: integers, floats, strings, Booleans, lists, tuples, dictionaries, etc.
  • Variables in Python have scope, which defines where they can be accessed from in the code.
  • Variables declared inside a function have local scope, while variables declared outside the functions have global scope.
# Integer variable
age = 25

# Float variable
height = 5.8

# String variable
name = "Alice"

# Boolean variable
is_student = True

# Multiple assignment
x = y = z = 10 # Assigns the value 10 to all three variables

# Variable reassignment
x = 5
y = 3
z = x + y # z is now assigned the value 8

# Using variables in print statements
print("Name:", name)
print("Age:", age)
print("Height:", height)
print("Is student?", is_student)
print("Sum of x and y:", z)

Loop in Python

In Python, loops are like repetitive tasks that help you do something over and over again without having to write the same code multiple times. There are main types of loops in Python: ‘for’ loops and ‘while’ loops.

For loop

A for loop is like a magic spell that repeats a set of instructions for each iten in a collection, like a list or a range of numbers.

# Let's say you have a list of fruits:
fruits = ["apple", "banana", "cherry"]

# You can use a for loop to print each fruit:
for fruit in fruits:
print(fruit)

This loop goes through each fruit in the list and prints it. It starts with ‘apple’ then ‘banana’ and finally ‘cherry’.

While loop

A while loop is like a never-ending adventure that keeps going until a condition is no longer true.

# Let's say you want to count from 1 to 5:
count = 1
while count <= 5:
print(count)
count = count + 1

This loop starts with a count of 1. As long as the count is less than or equal to 5, it prints the current count and then increases int by 1. It repeats this until the count becomes 6, at which point the condition ‘count ≤ 5’ becomes false, and the loop stops.

Conditional Statements

Conditional statements are like decision-making tools that help your code choose what to do based on certain conditions. The two main types of conditional statements in Python are ‘if’ statements and ‘else’ statement.

If Statement

An ‘if’ statement is like a gatekeep er that checks if a condition is true, and if it is, it lets your code do something.

# Let's say you want to check if a number is greater than 10:
number = 15
if number > 10:
print("The number is greater than 10!")

In this example, the ‘if’ statement checks if the ‘number’ variable is greater than 10. If it is, the code inside the ‘if’ block get executed, and you’ll see the message “The number is greater than 10!” printed.

Else Statement

An ‘else’ statement is like a backup plan that gets executed when the condition in an ‘if’ statement is not true.

# Let's say you want to check if a number is greater than 10, and if it's not, you want to do something else:
number = 5
if number > 10:
print("The number is greater than 10!")
else:
print("The number is not greater than 10.")

In this example, if the ‘number’ variable is not greater than 10, the code inside the ‘else’ block gets executed and you’ll see the message “The number is not greater than 10.”

Basic Data Structures in Python

List

A list in python is like a collection of items stored in particular order. It’s like having a backpack where you can put different things.

# Let's create a list of fruits:
fruits = ["apple", "banana", "cherry"]

# You can access individual items in the list using their index:
print(fruits[0]) # Prints "apple"
print(fruits[1]) # Prints "banana"
print(fruits[2]) # Prints "cherry"

# You can also add new items to the list:
fruits.append("orange")

# And remove items from the list:
fruits.remove("banana")

# Lists are flexible and can hold different types of items, like strings, numbers, or even other lists.

Tuples

A tuples is similar to a list, but it’s immutable, which means you can’t change its contents after creating it. It’s like havinga sealed envelope with some information inside.

# Let's create a tuple of coordinates:
point = (10, 20)

# You can access individual items in the tuple using their index:
print(point[0]) # Prints 10
print(point[1]) # Prints 20

# Tuples are useful when you want to store a fixed set of values that shouldn't change.

Dictionaries

A dictionary in Python is like a phone book where you can look up information using a particular key. It’s like having a key-value pair where each key is unique.

# Let's create a dictionary of student information:
student = {"name": "Alice", "age": 10, "grade": 5}

# You can access values in the dictionary using their keys:
print(student["name"]) # Prints "Alice"
print(student["age"]) # Prints 10

# You can also add new key-value pairs to the dictionary:
student["school"] = "ABC School"

# And remove key-value pairs from the dictionary:
del student["grade"]

# Dictionaries are great for storing and retrieving data based on specific keys.

Arrays

In Python, arrays are similar to lists, but they are usually used in numerical computations. You can use the ‘numpy’ library to work with arrays effectively. Arrays are optimized for numerical operations and can efficiently handle large datasets.

import numpy as np

# Let's create an array of numbers:
numbers = np.array([1, 2, 3, 4, 5])

# You can perform various numerical operations on arrays:
print(numbers.sum()) # Prints the sum of all numbers
print(numbers.mean()) # Prints the mean of all numbers

# Arrays are powerful for numerical computations and data manipulation.

Control Flow

Break Statement

The ‘break’ statement is like an emergency exit that allows you to immediately stop th execution of a loop, even if the loop condition hasn’t been fully satisfied.

# Let's say you want to find the first occurrence of a number in a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
target = 5

for number in numbers:
if number == target:
print("Number found!")
break # Stop the loop as soon as the number is found

In this example, when the ‘target’ number is found in the ‘numbers’ list, the ‘break’ statement immediately exits the loop, even though there might be more numbers in the list.

Continue Statement

The ‘continue’ statement is like a skip button that allows you to skip the current iteration of a loop and move on to the next one.

# Let's say you want to print only odd numbers from a list:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for number in numbers:
if number % 2 == 0: # Check if the number is even
continue # Skip even numbers and move to the next iteration
print(number)

In this example, when an even number is encountered, the ‘continue’ statement skips printing it and moves on to the next iteration of the loop, ensuring that only odd numbers are printed.

Private Variables in Python

Private variables are like secrets that are meant to be hidden from the outside world. They’re accesible only inside the class where they are defined.

In python, private variables are indicated by prefixing the variable name with double underscores ‘__’.

class Car:
def __init__(self, brand, model):
self.__brand = brand # Private variable
self.__model = model # Private variable

def get_brand(self):
return self.__brand

def get_model(self):
return self.__model

# Create a Car object
my_car = Car("Toyota", "Camry")

# Accessing private variables directly from outside the class will result in an error:
# print(my_car.__brand) # This will raise an AttributeError

# But you can access them using public methods:
print(my_car.get_brand()) # Prints "Toyota"
print(my_car.get_model()) # Prints "Camry"

In this example ‘__brand’ and ‘__model’ are private variables of the ‘Car’ class. They cannot be accessed directly from outside the class. However, public methods (‘get_brand()’ and ‘get_model()’) are provided to access these private variables indirectly.

String Formatting

String formatting in Python allows you to create formatted strings with placeholders that can be replaced with values. There are multiple ways to format strings in Python, including using the “%” operator, the ‘format()’ method and f-strings (formatted string literals).

Let’s discuss each method.

Using ‘%’ Operator

name = "Alice"
age = 25
formatted_string = "My name is %s and I am %d years old." % (name, age)
print(formatted_string)

Using ‘format()’ Method

name = "Bob"
age = 30
formatted_string = "My name is {} and I am {} years old.".format(name, age)
print(formatted_string)

Using f-strings (Formatted String Literals)

name = "Charlie"
age = 35
formatted_string = f"My name is {name} and I am {age} years old."
print(formatted_string)

All three methods achieve same result, but f-strings are considered more modern and preferred due to their simplicity and readability.

List Comprehension in Python

List comprehension in Python is a concise way to create lists based on existing lists or other iterable objects. It allows you to write compact and readable code for creating lists in a single line.

The basic syntax of list comprehension is:

new_list = [expression for item in iterable if condition]

Here is a breakdown of each part:

  • ‘expression’: The expression to be evaluated and added to the new list.
  • ‘item’ The variable representing each item in iterable.
  • ‘iterable’: The existing list, tuple, string, or any iterable object.
  • ‘condition’: An optional condition that filters the items added to the new list.

Here are some examples of list comprehension:

Creating a list of squares:

numbers = [1, 2, 3, 4, 5]
squares = [x**2 for x in numbers]
# squares will be [1, 4, 9, 16, 25]

Filtering even numbers:

numbers = [1, 2, 3, 4, 5]
even_numbers = [x for x in numbers if x % 2 == 0]
# even_numbers will be [2, 4]

Converting String to uppercase:

words = ["hello", "world", "python"]
uppercase_words = [word.upper() for word in words]
# uppercase_words will be ['HELLO', 'WORLD', 'PYTHON']

Creating a list of tuples:

numbers = [1, 2, 3]
squares = [(x, x**2) for x in numbers]
# squares will be [(1, 1), (2, 4), (3, 9)]

Lamda Functions

Lamda functions, also known as anonymous functions or lambda expressions, are small, inline functions that can have any number of arguments but can only have one expression. They are particularly useful when you need a simple function for short period of time, such as for use as arguments in higher-order functions like ‘map()’ , ‘filter()’ and ‘sorted()’ or in situations where defining a named function is unnecessary or cumbersome.

lambda arguments: expression

Lamda functions can be used wherever a function object is required. Here are some examples:

Using lambda with ‘map()’:

numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
# squared will be [1, 4, 9, 16, 25]

Using lambda with ‘filter()’:

numbers = [1, 2, 3, 4, 5]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
# even_numbers will be [2, 4]

Using lambda with ‘sorted()’:

words = ["apple", "banana", "cherry", "date"]
sorted_words = sorted(words, key=lambda x: len(x))
# sorted_words will be ["date", "apple", "banana", "cherry"]

Conclusion

Overall, Python’s simplicity, readability and extensive library ecosystem make it a popular choice for a wide range of applications, including web development, data analysis, machine learning, automation and more. Whether you’re a beginner learning to code or an experienced developer tackling complex problems, Python offers tools and features to meet your needs effectively.

No comments:

Post a Comment