Data Analytics with Python
Syllabus
What is python?
Python is a general purpose, dynamic, high-level, and interpreted
programming language. It supports Object Oriented programming approach to
develop applications. It is simple and easy to learn.
- Easy to learn and use
- Interpreted language
- Object-Oriented Programming Language
- Open-Source Language
- Dynamic Memory Allocation
- Case Sensitive
Uses of python:
- System scripting
- Web development
- Software development
- Complex mathematics
Data type:
A data type represents the kind of value that tells what operations can
be performed on that data.
Eg: int,float,str
- Integer - Integer value can
be like 10, 2, 29, -20, -150 etc. Python has no restriction on the length
of an integer. Its value belongs to int .
- Float - Float is used to
store floating-point numbers like 1.9, 9.902, 15.2, etc.
Its value belongs to float
- String: The string can
be defined as the sequence of characters represented in the quotation
marks. Its value belongs to str
How to know the type of the data in
python?
Python provides us the type() function, which returns the type
of the variable passed. To reveal the data type of the given data.
>type(<value>)
Eg: type (11): int
What is type casting in python?
Changing the type of expression in python is called type casting.
>float(<value>): decimal value
eg:
1.float (1)
=>1.0
2.int (3.14)
=>3
What is Boolean value?
A Boolean is a data type which can take only 2 values. (True or False).
bool(1) =
True int(True) =
1
bool(0) =
False int(False)
= 0
Expressions and Variables:
An expression is a combination of variables operands and operators that python evaluates to produce another value.
A variable is used to store the value in it. eg: a = 10, here 'a' is a variable.
Operators: Operators are the symbols that are assigned to values to perform mathematical and logical operations.
Operand: Operand is the value to which the operators are assigned.
Compounds of data types:
1. List
- Definition: An ordered sequence, mutable collection of elements. It is represented by '[ ]'.
- Syntax:
list = [1, 2, 3, 'hello'] - Key Features:
- Supports indexing and slicing.
- Allows duplicate elements.
- Elements can be of different data types.
2. Tuple
- Definition: An ordered, immutable collection of elements. Tuples are written as comma ',' separated elements within parenthesis '( )'
- Syntax: tuple = (1,2,3,'a','b')
- Key Features:
- Supports indexing and slicing.
- Immutable, making it suitable for fixed collections.
- Allows duplicate elements.
3. Set
- Definition: An unordered, mutable collection of unique elements. We can convert a list inti set by using type casting.
- Syntax:
set = {1, 2, 3, 'hello'} - Key Features:
- No duplicate elements are allowed.
- Does not support indexing or slicing.
- Useful for membership tests and eliminating duplicates.
4. Dictionary
- Definition: An unordered, mutable collection of key-value pairs.
- Syntax:
dict = {'key1': 'value1', 'key2': 'value2'} - Key Features:
- Keys must be unique and immutable.
- Values can be of any data type and may duplicate.
- Ideal for storing data in a mapping format.
Condition and Branching:
A condition is a task given to a program and the condition takes the
program to 2 possible values.
True or False
If the condition is satisfied then the program can go further and if the condition is not satisfied the program skips the condition
Branching statements in Python allows to control the flow of your
program based on certain conditions.
1.if statement: if the statement is true then the condition in the parenthesis will be executed.
>if(condition):
statement
2.else statement: if the statement is false then the else statement is executed.
>if(condition):
statement
else:
statement
3.elseif (elif) statement: when the first if statement isn't true, but you want to check for another condition.
>if(condition):
Statement
elif(condition):
statement
else:
statement
Loops:
Loops are a block of code which continue itself until a certain condition is met. Loops are used to minimize the code and make the code more manageable.
while loop: a while loop is used to execute a block of statements repeatedly until a given condition is satisfied but when the condition becomes false the loop executes the next line of the loop.
- A while loop repeatedly executes a block of code as long as a given condition is true.
- It does not have a fixed number of iterations but continues executing until the condition becomes false.
- The condition is checked before each iteration, and if it's false initially, the code block is skipped entirely.
- The condition is typically based on a variable or expression that can change during the execution of the loop.
- It provides more flexibility in terms of controlling the loop's execution based on dynamic conditions.
syntax:
while(<expression>):
statement
eg:
a= 0
while (a< 3):
a =a + 1
print ("while loop")
for loop: for loop runs in the given range. A for loop is used for iterating over a sequence.
Key point of For Loop:
- A for loop iterates over a sequence (such as a list, string, or range) or any object that supports iteration.
- It has a predefined number of iterations based on the length of the sequence or the number of items to iterate over.
- It automatically handles the iteration and does not require maintaining a separate variable for tracking the iteration count.
- It simplifies the code by encapsulating the iteration logic within the loop itself.
- It is commonly used when you know the exact number of iterations or need to iterate over each item in a collection.
syntax:
for <var> in range():
statement
eg:
a = 10
for
i in range (0,10):
print(i)
Break Statement :
The break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one. Break is used to abort the current execution of the program and the control goes to the next line after the loop.
a=1
def add(b):
return a+b
c=add(10)
=> 11
String Operations:
A string has escape sequences by which we can change the line or to make space between 2 strings.
These are known as string escape sequence.
- \n : it takes the string to the next line.
- \t : it creates a space in between the strings.
String Method:
The strings in python have functions that can change the strings accordingly.
- upper() : make the whole string into uppercase.
- replace() : it replaces the string to the new string assigned.
- find() : the find function breaks the whole string into single characters and arrange them in array and finds thee exact string character we need.
Comments
Post a Comment