Python Constructs
1. Functions in Python
A function in Python is a collection of statements grouped under a name. You can use it whenever you want to execute all those statements at a time.
You can call it wherever you want and as many times as you want in a program. A function may return a value.
2. Classes in Python
Python is an object-oriented language. It supports classes and objects.
A class is an abstract data type. In other words, it is a blueprint for an object of a certain kind. It holds no values.
An object is a real-world entity and an instance of a class.
3. Modules in Python
Python module is a collection of related classes and functions.
We have modules for mathematical calculations, string manipulations, web programming, and many more.
4. Packages in Python
Python package is a collection of related modules. You can either import a package or create your own.
Python has a lot of other constructs. These include control structures, functions, exceptions, etc
1. Functions in Python
A function in Python is a collection of statements grouped under a name. You can use it whenever you want to execute all those statements at a time.
You can call it wherever you want and as many times as you want in a program. A function may return a value.
2. Classes in Python
Python is an object-oriented language. It supports classes and objects.
A class is an abstract data type. In other words, it is a blueprint for an object of a certain kind. It holds no values.
An object is a real-world entity and an instance of a class.
3. Modules in Python
Python module is a collection of related classes and functions.
We have modules for mathematical calculations, string manipulations, web programming, and many more.
4. Packages in Python
Python package is a collection of related modules. You can either import a package or create your own.
Python has a lot of other constructs. These include control structures, functions, exceptions, etc
π20β€3
π What is Python Data Structures?
You can think of a data structure as a way of organizing and storing data such that we can access and modify it efficiently.
We have primitive data types like integers, floats, Booleans, and strings.
π What is Python List?
A list in Python is a heterogeneous container for items. This would remind you of an array in C++, but since Python does not support arrays, we have Python Lists.
π Python Tuple
This Python Data Structure is like a, like a list in Python, is a heterogeneous container for items.
But the major difference between the two (tuple and list) is that a list is mutable, but a tuple is immutable.
This means that while you can reassign or delete an entire tuple, you cannot do the same to a single item or a slice.
π Python Dictionaries
Finally, we will take a look at Python dictionaries. Think of a real-life dictionary. What is it used for? It holds word-meaning pairs. Likewise, a Python dictionary holds key-value pairs. However, you may not use an unhashable item as a key.
To declare a Python dictionary, we use curly braces. But since it has key-value pairs instead of single values, this differentiates a dictionary from a set.
You can think of a data structure as a way of organizing and storing data such that we can access and modify it efficiently.
We have primitive data types like integers, floats, Booleans, and strings.
π What is Python List?
A list in Python is a heterogeneous container for items. This would remind you of an array in C++, but since Python does not support arrays, we have Python Lists.
π Python Tuple
This Python Data Structure is like a, like a list in Python, is a heterogeneous container for items.
But the major difference between the two (tuple and list) is that a list is mutable, but a tuple is immutable.
This means that while you can reassign or delete an entire tuple, you cannot do the same to a single item or a slice.
π Python Dictionaries
Finally, we will take a look at Python dictionaries. Think of a real-life dictionary. What is it used for? It holds word-meaning pairs. Likewise, a Python dictionary holds key-value pairs. However, you may not use an unhashable item as a key.
To declare a Python dictionary, we use curly braces. But since it has key-value pairs instead of single values, this differentiates a dictionary from a set.
π11β€5
What is Python Loop?
When you want some statements to execute a hundred times, you donβt repeat them 100 times.
Think of when you want to print numbers 1 to 99. Or that you want to say Hello to 99 friends.
In such a case, you can use loops in python.
Here, we will discuss 4 types of Python Loop:
Python For Loop
Python While Loop
Python Loop Control Statements
Nested For Loop in Python
Python While Loop
A while loop in python iterates till its condition becomes False. In other words, it executes the statements under itself while the condition it takes is True.
Python For Loop
Python for loop can iterate over a sequence of items. The structure of a for loop in Python is different than that in C++ or Java.
That is, for(int i=0;i<n;i++) wonβt work here. In Python, we use the βinβ keyword.
Nested for Loops in Python
You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while.
Or you can put a loop inside a loop inside a loop. You can go as far as you want.
Loop Control Statements in Python
Sometimes, you may want to break out of normal execution in a loop.
For this, we have three keywords in Python- break, continue, and Python
When you want some statements to execute a hundred times, you donβt repeat them 100 times.
Think of when you want to print numbers 1 to 99. Or that you want to say Hello to 99 friends.
In such a case, you can use loops in python.
Here, we will discuss 4 types of Python Loop:
Python For Loop
Python While Loop
Python Loop Control Statements
Nested For Loop in Python
Python While Loop
A while loop in python iterates till its condition becomes False. In other words, it executes the statements under itself while the condition it takes is True.
Python For Loop
Python for loop can iterate over a sequence of items. The structure of a for loop in Python is different than that in C++ or Java.
That is, for(int i=0;i<n;i++) wonβt work here. In Python, we use the βinβ keyword.
Nested for Loops in Python
You can also nest a loop inside another. You can put a for loop inside a while, or a while inside a for, or a for inside a for, or a while inside a while.
Or you can put a loop inside a loop inside a loop. You can go as far as you want.
Loop Control Statements in Python
Sometimes, you may want to break out of normal execution in a loop.
For this, we have three keywords in Python- break, continue, and Python
π12β€5π1
Python Cheat Sheet.pdf
677.7 KB
This cheat sheet includes basic python required for data analysis excluding pandas, numpy & other libraries
β€8π2π1
Which topic to cover next?
Anonymous Poll
16%
Functions
5%
Loops/ conditional statements
5%
Classes & Objects
18%
Pandas
9%
Numpy
4%
Matplotlib
1%
Scipy
5%
Scikit learn
37%
All of the above
π11β€1
π List comprehensions: List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list.
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.
Without list comprehension you will have to write a for statement with a conditional test inside:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
With list comprehension you can do all that with only one line of code:
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.
Without list comprehension you will have to write a for statement with a conditional test inside:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
With list comprehension you can do all that with only one line of code:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]
print(newlist)
π21
Python for Data Analysts
π List comprehensions: List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list. Example: Based on a list of fruits, you want a new list, containing only the fruits with the letter "a" in the name.β¦
Syntax:
newlist = [expression for item in iterable if condition == True]
The return value is a new list, leaving the old list unchanged.
The condition is like a filter that only accepts the items that valuate to True. The condition is optional and can be omitted.
The iterable can be any iterable object, like a list, tuple, set etc.
The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list.
newlist = [expression for item in iterable if condition == True]
The return value is a new list, leaving the old list unchanged.
The condition is like a filter that only accepts the items that valuate to True. The condition is optional and can be omitted.
The iterable can be any iterable object, like a list, tuple, set etc.
The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list.
π5β€1
Python Lambda:
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
lambda arguments : expression
The expression is executed and the result is returned.
Use lambda functions when an anonymous function is required for a short period of time.
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
lambda arguments : expression
The expression is executed and the result is returned.
Use lambda functions when an anonymous function is required for a short period of time.
π6
Python for Data Analysts
Python Lambda: A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. lambda arguments : expression The expression is executed and the result is returned. Use lambda functionsβ¦
Example:
Add 10 to argument a, and return the result:
Output will be 15
Add 10 to argument a, and return the result:
x = lambda a : a + 10
print(x(5))
Output will be 15
π6
What will be the output of this code in python?
x = lambda a, b : a * b
print(x(5, 6))
x = lambda a, b : a * b
print(x(5, 6))
Anonymous Quiz
5%
11
91%
30
2%
-1
2%
1
π4β€2
Output of this code?
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
x = lambda a, b, c : a + b + c
print(x(5, 6, 2))
Anonymous Quiz
4%
11
4%
8
2%
7
90%
13
π5
Python Generators
In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated over.
Generators are useful when we want to produce a large sequence of values, but we don't want to store all of them in memory at once.
There is a lot of complexity in creating iteration in Python; we need to implement iter() and next() method to keep track of internal states.
It is a lengthy process to create iterators. That's why the generator plays an essential role in simplifying this process. If there is no value found in iteration, it raises StopIteration exception.
In Python, a generator is a function that returns an iterator that produces a sequence of values when iterated over.
Generators are useful when we want to produce a large sequence of values, but we don't want to store all of them in memory at once.
There is a lot of complexity in creating iteration in Python; we need to implement iter() and next() method to keep track of internal states.
It is a lengthy process to create iterators. That's why the generator plays an essential role in simplifying this process. If there is no value found in iteration, it raises StopIteration exception.
π7β€1
Python Variables: How to Define/Declare String Variable Types
What is a Variable in Python?
A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
Python Variable Types
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.
How to Declare and use a Variable
Let see an example. We will define variable in Python and declare it as βaβ and print it.
What is a Variable in Python?
A Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.
Python Variable Types
Every value in Python has a datatype. Different data types in Python are Numbers, List, Tuple, Strings, Dictionary, etc. Variables in Python can be declared by any name or even alphabets like a, aa, abc, etc.
How to Declare and use a Variable
Let see an example. We will define variable in Python and declare it as βaβ and print it.
1 a=100
2 print (a)
π19
Big Data Analytics_ A Hands-On Approach ( PDFDrive ).pdf
108.4 MB
Big Data Analytics_ A Hands-On Approach ( PDFDrive ).pdf
π₯°12