Wednesday, October 30, 2024

Python Basic to Advanced Learnings ( Day 3 )

Algorithm in python

Definition

An algorithm is a list of steps or statements that can be converted into code and executed by a computer.

Purpose

Algorithms are the foundation of programming and are used to solve problems by taking in input, processing it, and providing an answer.

Characteristics

Algorithms are not language-specific and can be implemented in several programming languages. They are resource- and problem-dependent, but share some common code constructs, such as flow-control (if-else) and loops (do, while, for).

Examples

Some well-known Python algorithms include tree traversal, sorting, and searching.

Real time Example

If we are going to prepare a coffee,

Preparation of coffee – Algorithm ( Set of instructions )

Sugar, Milk, Coffee Powder – Data ( It is stored in Memory which requires some Space )

Data Structure

·        Data Structures are a way of organizing data so that it can be accessed more efficiently depending upon the situation.

·        Data Structures are fundamentals of any programming language around which a program is built.

·        Python helps to learn the fundamental of these data structures in a simpler way as compared to other programming languages.

·        Any program has no meaning without the data.

For example, we have the program to send an email, then this program is of no use if we don’t have any email address or the body of the email.

And it is very critical how well we organize or store the data.

For the purpose of data management we have the data structures.

·        To store any data, we use variables – if we have a large amount of data and we want to store it so for this purpose we have to use data structures.

·        Suppose if we have some mobile numbers and we want to send messages on them, so for sorting the mobile numbers we cannot use multiple variables for each mobile number.

·        If we use multiple variables then all data will be allocated to the random locations on the RAM

·        It will take a lot of space in the RAM.

 

Arrays

·        Arrays are used to store multiple values in a single variable.

·        Arrays are used to store the data in a sequential manner.

·        Another Example –



·        An array can hold many values under a single name, and you can access the values by referring to an index number.









·        In python, there are two ways to create the array – one is using the tuple and other one is using list.

Tuple

·        Tuple is a built-in data type in python used to store collection of data, the other 3 are list, set and dictionary

·        A tuple is a collection of data which is ordered and immutable – it allow duplicate values

·        Tuples are written with rounded brackets “()”.

·        Immutable means that we cannot change the data-  add or remove data from the tuple.




 






Accessing element in array

·        To access the elements in a array, you have to get the position of the element and call like below example






List

·        List is built-in data types in Python used to store collections of data.

·        List is a mutable, or changeable, ordered sequence of elements.

·        They can hold a various of data types, including integers, floats, strings, and even other lists.

·        We can create a list in Python using square brackets [] or by using the list() constructor.



·        In list we can change data at any position as it is mutable.



·        In python we can do row-wise operations with lists and tuples.



·        The same can be done using the python list slicing.

In the above example,

1:4 means it starts from the 1st index and will go to the 3rd, it excludes 0th element in the list.

This is known as slicing in python

·        To pick the last data of the list you can use -1. ( Negative Indexing )



·        To get all elements in the list you have to do like below,

Using [:] & [::] without specifying any start, end, or step returns all elements of the list.





·        To get all items before/after a specific position

To get all the items from a specific position to the end of the list, we can specify the start index and leave the end blank.

And to get all the items before a specific index, we can specify the end index while leaving start blank.



·        To get all items in between two specific positions



·        To get items at specified intervals,

In this example,

# Get every second element from the list starting from the beginning

# Get every second element from the list starting from index 0 to 3(exclusive)



·        Out of bound slicing, list slicing allows out-of-bound indexing without raising errors.

If we specify indices beyond the list length then it will simply return the available items.

In this example, even if we have five elements if we try to access 3rd till 6th element, if we have only 5 elements in the list – it will get only the items till the end without any error



·        To reverse a list using slicing,



·        Suppose if we want to store the data like this we will be using Nested list.



Note : If we want to retrieve only the mobile number from the above list, will it be possible ? without using any libraries or for loop ?

Answer : Not Possible

·        In this case we can use numpy or for loop ( list comprehension ) as a solution

Nested Lists

·        A list within another list is referred to as a nested list in Python.

·        We can also say that a list that has other lists as its elements is a nested list.

·        To access the elements in this nested list we use indexing.



·        The above list elements can be accessed by the below methods.



1D Array vs 2D Array

·        Arrays can be created in 1D or 2D.   

·        1D arrays are just one row of values, while 2D arrays contain a grid of values that has several rows/columns.











List Methods

·        Below are some widely used list methods,

ü  append(): adds an element to the end of the list.

ü  insert(): adds an element at a specified position in the list.

ü  remove(): removes the first occurrence of a specified element from the list.

ü  pop(): removes the element at a specified position in the list and returns it.

ü  sort(): sorts the elements in the list in ascending order.

ü  reverse(): reverses the order of the elements in the list.

ü  count(): returns the number of times a specified element appears in the list.

ü  index(): returns the index of the first occurrence of a specified element in the list.

ü  extend(): extends the list by appending elements from another iterable.










To access count() in list






To access count() in nested list




























Lists vs Tuples



 

Tuple Methods

·        Tuple has only limited number of built-in methods

ü  count()

ü  Index()



Sunday, October 20, 2024

Python Basic to Advanced Learnings ( Day 2 )

 

Variables

·        Python Variable is containers that store values.

·        Python is not “statically typed”.

·        We do not need to declare variables before using them or declare their type.

·        A variable is created the moment we first assign a value to it.

·        A Python variable is a name given to a memory location.

Restrictions of Python Variables

·        A Python variable name must start with a letter or the underscore character.

·        A Python variable name cannot start with a number.

·        A Python variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).

·        Variable in Python names are case-sensitive (name, Name, and NAME are three different variables).

·        The reserved words(keywords) in Python cannot be used to name the variable in Python. ( example and,or,if )

Examples of Variables

Declaring and initialization of variables

= ( Equal to ) – is a assignment operator










Redeclaring Variables in python








Assigning Value to Multiple Variables








Assigning Different Values to Multiple Variables








Same Variable Name for Different DataTypes


 
                                                                                   





           

+ operator in Variable                

It will add if it integer, if string concatenate









Different DataTypes in + Operator



 

 

 


 

Datatypes

Data types represent the value type that helps understand what operations you can perform on a particular piece of data.

Built-in Datatypes

Ø  Text Type:    str

Ø  Numeric Types:        int, float, complex

Ø  Sequence Types:      list, tuple, range

Ø  Mapping Type:         dict

Ø  Set Types:   set, frozenset

Ø  Boolean Type:          bool

Ø  Binary Types:            bytes, bytearray, memoryview

Ø  None Type: NoneType

Example of Datatypes ( type () function )

type() function – is a built-in function that returns the type of the objects/data elements stored in any data type.










Libraries in python

·        A Python library is a collection of pre-written code modules that can be used to perform specific tasks in Python programs.

·        Libraries can contain functions, classes, constants, and data values.

·        They can help programmers:

Avoid reinventing the wheel: Libraries can help programmers expedite development by providing reusable code.

Build more robust applications: Libraries can help programmers build more complex applications.

Make everyday tasks more efficient: Libraries can make everyday tasks more efficient. For example, a library like Seaborn can generate visualizations with just one line of code.

Example : Numpy, Panda, Matpolib, pyttsx3, pywhatkit, bokeh

PYTTSX3 Library installation

·        pyttsx3 is a text-to-speech conversion library in Python.

https://pypi.org/project/pyttsx3/

tts – text to speech

pip install pyttsx3 – For installation






pip list | findstr pyttsx3 – to list the installed libraries




Import library, define the library, call the say function from the library



 




Pywhatkit Library Installation

·        pywhatkit is a Python library for sending WhatsApp messages at a certain time, it has several other features too.

https://pypi.org/project/pywhatkit/      

pip install pywhatkit – For installation






pip list | findstr pywhatkit





Help() function - is a built-in function that provides information about modules, classes, functions, and modules.







Python Basic to Advanced Learnings ( Day 3 )

Algorithm in python Definition An algorithm is a list of steps or statements that can be converted into code and executed by a computer....

Popular Posts