How to Learn Python for data analytics (Step-By-Step) in 2023
Here is a step-by-step guide to learning Python for data analytics in 2023:
Table of Contents
ToggleHere is a step-by-step guide to learning Python for data analytics in 2023:
Start by learning the basic syntax and concepts of Python, such as variables, data types, loops, and control structures. Best to Join Vista Academy
Next, familiarize yourself with the Python standard library, particularly the math, statistics, and date/time modules. These modules contain functions and methods that will be useful for data analytics tasks.
After you have a solid foundation in Python, it’s time to start working with data. You’ll need to learn how to import, clean, and manipulate data using libraries like NumPy, Pandas, and Matplotlib.
Once you’re comfortable with the basics of data manipulation, you can start learning more advanced techniques such as machine learning, data visualization, and data analysis. There are many online tutorials, courses, and resources available to help you learn these skills.
As you learn, it’s important to practice your skills by working on real-world projects. This will help you apply what you’ve learned and give you a chance to demonstrate your knowledge to potential employers.
Finally, consider getting certified in Python for data analytics. This can help you stand out in the job market and demonstrate your expertise to employer
Introduction to Python for data Analytics
Python is a high-level, interpreted programming language. It was first released in 1991 and has become one of the most popular programming languages in the world, with a large and active community of users and developers. Python is known for its simplicity, readability, and flexibility, as well as its support for object-oriented, imperative, functional, and procedural programming styles. Python is used for a wide range of applications, including web development, scientific computing, data analysis, machine learning, and artificial intelligence.
The new oil is data. This statement demonstrates how data capture, storage, and analysis are at the heart of every modern IT system. It doesn’t matter if you’re making a business choice, forecasting the weather, investigating protein structures in biology, or creating a marketing strategy. All of these scenarios call for a multidisciplinary approach that includes the use of mathematical models, statistics, graphs, databases, and, of course, the commercial or scientific rationale that underpins the data analysis. As a result, we need a programming language that can handle all of these different data science requirements. Python stands out as one of these languages since it comes with a plethora of libraries and built-in capabilities that make it simple to meet the demands of data research.
Python has libraries
Python contains a number of libraries that contain a significant number of mathematical functions and analytical tools.
The following libraries will be used in this course:
Pandas – This library is used to do structured data operations such as importing CSV files, creating dataframes, and preparing data.
Numpy is a library for mathematicians. There’s an N-dimensional array object, linear algebra, and the Fourier transform, among other things.
Matplotlib is a library that allows you to visualise data.
SciPy is a Python library that includes linear algebra modules.
Comment in Python
# This is a comment on its own line
Variable in python
num = 100 #num is of type int str = "Chaitanya" #str is of type string
Identifiers - Variable name
Python Variable Example
num = 100 str = "VistaAcademy" print(num) print(str)practice in below
What is the meaning of a Python keyword?
A reserved term in Python is one that you can’t use as the name of a variable, class, function, or anything else. These keywords have a specific meaning and are used in the Python programming language for specific purposes.
Here is a list of the Python keywords. Enter any keyword to get more help.
False def if raise
None del import return
True elif in try
and else is while
as except lambda with
assert finally nonlocal yield
break for not
class from or
continue global pass
Example of Variable
num = 10 while num>5: print(num) num -= 1
Python data Type
- Integer: Integers are whole numbers. They can be positive, negative, or zero. For example: 2, -5, 0.
- Float: Floats are numbers with a decimal point. They can be positive, negative, or zero. For example: 3.14, -2.5, 0.0.
- String: Strings are sequences of characters. They can be defined using single quotes (‘) or double quotes (“). For example: ‘hello’, “world”, “123”.
- Boolean: Booleans represent one of two values: True or False. For example: True, False.
- List: Lists are collections of items that are ordered and changeable. They can contain elements of any data type. Lists are defined using square brackets ([]). For example: [1, 2, 3], [‘apple’, ‘banana’, ‘cherry’], [True, False, True].
- Tuple: Tuples are similar to lists, but they are immutable (i.e., they cannot be changed). They are defined using parentheses (()). For example: (1, 2, 3), (‘apple’, ‘banana’, ‘cherry’), (True, False, True).
- Dictionary: Dictionaries are collections of items that are unordered and changeable. They are organized using key-value pairs. Dictionaries are defined using curly braces ({}). For example: {‘name’: ‘John’, ‘age’: 30}, {1: ‘apple’, 2: ‘banana’, 3: ‘cherry’}, {True: ‘yes’, False: ‘no’}.
- Set: Sets are collections of items that are unordered and unchangeable. Sets are defined using curly braces ({}). For example: {1, 2, 3}, {‘apple’, ‘banana’, ‘cherry’}, {True, False}.
Numeric Types
Integer –
In Python 3, there is no upper bound on the integer number which means we can have the value as large as our system memory allowx = 20 #display x: print(x) #display the data type of x: print(type(x))
Float
It is automatically inferred based on the value we are assigning to a variable. For example here fnum is a float data type.# float number fnum = 34.45 print(fnum) print("Data Type of variable fnum is", type(fnum))
Complex Number
x = 1j#display x: print(x) #display the data type of x: print(type(x))
Python Data Type – String
s = "This is a String" s2 = 'This is also a String' # displaying string s and its type print(s) print(type(s))print(s2) print(type(s2))
Boolean Values
print(10 > 9) print(10 == 9) print(10 < 9)Example 2
a = 200 b = 330 if b > a: print("b is greater than a") else: print("b is not greater than a")
Operator in Python
+ addition
x = 5 y = 3 print(x + y)
-Subtraction
x = 5 y = 3 print(x - y)
*Multiplication
x = 6 y = 3 print(x * y)
/Division
x = 12 y = 7 print(x / y)
%Modulus
x = 5 y = 2 print(x % y)
**Exponentiation
x = 2 y = 5 #shows remender of division print(x ** y) #same as 2*2*2*2*2
//Floor division
x = 15 y = 2 print(x // y) #the floor division // rounds the result down to the nearest whole number
Python Assignment Operators
x +=
x = 5 x += 3 print(x)#answer will be 8
-=
x = 5 x -= 3 print(x)#answer will be 2
*=
x = 5 x *= 3 print(x)#answer will be 15
/=
x = 5 x /= 3 print(x)
%=
x = 5 x%=3 print(x)#shows remainder 2
//
x = 5 x//=3 print(x)#shows result in integer 2
**=
x = 5 x **= 3 print(x)#mutiply 5*5*5
&=
x = 5 x &= 3 print(x)
Python Logical Operator
and
Logical AND: True if both the operands are true.
or
Logical OR: True if either of the operands is true x or y
# Print a and b is False
print(a and b)
# Print a or b is True
print(a or b)
# Print not a is False
print(not a)
Data Structure in Python
Data structures in Python are ways of organizing and storing data in a structured and efficient manner. They provide different ways to store, access, and manipulate data based on their properties and intended use cases. Python offers several built-in data structures, each with its own advantages and use cases. Some common data structures in Python are:
Python Data Type – List
# list of integers lis1 = [1, 2, 3, 4, 5] # prints entire list print(lis1)
# list of strings lis2 = ["Apple", "Orange", "Banana"] # loop through list elements for x in lis2: print (x)
# List of mixed type elements lis3 = [20, "Chaitanya", 15, "BeginnersBook"] ''' Print a specific element in list indexes start with zero ''' print("Element at index 3 is:",lis3[3])
Python Data Type – Tuple
In Python, a tuple is an unchanging data type, which means it cannot be modified. It’s a list of elements that are separated by commas and enclosed in round brackets.
t1 = (1, 2, 3, 4, 5) # prints entire tuple print(t1)
# tuple of strings
t2 = ("hi", "hello", "bye") # loop through tuple elements for s in t2: print (s)
# tuple of mixed type elements t3 = (2, "Lucy", 45, "Steve") ''' Print a specific element indexes start with zero ''' print(t3[2])
Python data type Dictionary
my_dict = {'name': 'John', 'age': 30, 'city': 'New York'} print(my_dict) {'name': 'John', 'age': 30, 'city': 'New York'} name = my_dict['name'] print(name)John You can also add, remove, and modify the elements in a dictionary:
my_dict['age'] = 25 print(my_dict) {'name': 'John', 'age': 25, 'city': 'New York'}<
my_dict[‘phone’] = ‘555-555-5555’ print(my_dict)
{'name': 'John', 'age': 25, 'city': 'New York', 'phone': '555-555-5555'}output
del my_dict['phone'] print(my_dict) {'name': 'John', 'age': 25, 'city': 'New York'}
Sets:
- Sets are unordered collections of unique items.
- They do not allow duplicate values.
- Useful for checking membership and performing set operations (union, intersection, etc.).
- Defined using curly braces: my_set = {1, 2, 3}
Creating a Set:
You can create a set using curly braces {} or the set() constructor. Sets automatically remove duplicate elements, so only unique elements are stored.# Creating a set using curly braces my_set = {1, 2, 3, 4, 4, 5} print(my_set) # Output: {1, 2, 3, 4, 5} # Creating a set using the set() constructor another_set = set([3, 4, 5, 6, 6]) print(another_set) # Output: {3, 4, 5, 6}
Adding Elements to a Set:
my_set = {1, 2, 3, 4, 5} my_set.add(6) print(my_set) # Output: {1, 2, 3, 4, 5, 6}
Removing Elements from a Set:
You can remove an element from a set using the remove() method. If the element doesn’t exist in the set, it will raise a KeyErrormy_set = {1, 2, 3, 4, 5} my_set.remove(4) print(my_set) # Output: {1, 2, 3, 5}
Checking Membership in a Set:
my_set = {1, 2, 3, 4, 5} print(3 in my_set) # Output: True print(6 in my_set) # Output: False
Set Operations:
set_a = {1, 2, 3, 4, 5} set_b = {4, 5, 6, 7, 8} # Union of two sets union = set_a | set_b print(union) # Output: {1, 2, 3, 4, 5, 6, 7, 8} # Intersection of two sets intersection = set_a & set_b print(intersection) # Output: {4, 5} # Difference between two sets difference = set_a - set_b print(difference) # Output: {1, 2, 3} # Symmetric difference (elements in either set, but not both) symmetric_diff = set_a ^ set_b print(symmetric_diff) # Output: {1, 2, 3, 6, 7, 8}
if statement in python
x = 10 if x > 5: print("x is greater than 5")In this example, the condition is x > 5. If the condition is true (in this case, it is), then the code inside the if block will be executed. In this case, the code inside the if block is print(“x is greater than 5”), which will print the string “x is greater than 5” to the console. You can also include an else clause in an if statement to specify a block of code that should be executed if the condition is false:
x = 3 if x > 5: print("x is greater than 5") else: print("x is not greater than 5")n this example, the condition is x > 5, which is false. Therefore, the code inside the else block will be executed, which will print the string “x is not greater than 5” to the console. You can also use the elif keyword to specify additional conditions that should be checked if the initial if condition is false. For example
x = 3 if x > 5: print("x is greater than 5") elif x == 5: print("x is equal to 5") else: print("x is less than 5")In this example, the initial condition is
x > 5
, which is false. The second condition, x == 5
, is also false. Therefore, the code inside the else
block will be executed, which will print the string "x is less than 5"
to the console. Loops in python
for i in range(10): print(i)This will output the numbers 0 through 9. Here is an example of a while loop that continues to execute as long as a certain condition is true:
x = 0 while x < 10: print(x) x += 1This will output the numbers 0 through 9, just like the for loop above. Both for loops and while loops can be controlled using the break and continue statements. The break statement will exit the loop completely, while the continue statement will skip the rest of the current iteration and move on to the next one. I hope this helps! Let me know if you have any questions.
While Loop in python
count = 5 while count > 0: print(count) count -= 1This will output the following: 5 4 3 2 1 It is important to include a way to modify the condition within the loop, or else the loop will run forever (this is known as an infinite loop). In the example above, the count variable is decremented by 1 each time the loop runs, so eventually count will become 0 and the condition will be False, terminating the loop. You can also use the break statement to terminate a while loop early, before the condition becomes False.
count = 5 while True: print(count) count -= 1 if count == 0: breakThis loop will run the same as the previous example, but it uses an infinite loop and the break statement to achieve the same result.
function in python
Step 1: Defining a Function
In Python, a function is a block of code that performs a specific task and can be reused throughout your program. To define a function, you use the def keyword followed by the function name and a pair of parentheses. Here’s an example:>def greet(name): print(f"Hello, {name}!")In this example, we’ve defined a function called greet that takes one parameter name.
Step 2: Function Parameters
Parameters are the inputs that a function receives. You can pass values (arguments) to a function when you call it. In the above example, name is a parameter.Step 3: Function Body
The indented block of code under the def statement is the function body. This is where you write the code that performs the desired task. In our greet function, the body contains a single line that prints a greeting.Step 4: Calling a Function
To use a function, you call it by using its name followed by parentheses. You can pass values (arguments) to the function if it expects any. Here’s how you call the greet function:greet("Alice")This will output: Hello, Alice!
Step 5: Return Statement (Optional)
Functions can also return values back to the caller using the return statement. Here’s an example:def add(a, b): return a + bIn this example, the add function takes two parameters (a and b) and returns their sum.
Step 6: Function Call and Return Value
When you call a function that uses the return statement, you can capture the returned value in a variable:result = add(5, 3) print(result) # This will print 8
Step 7: Default Parameters (Optional)
You can provide default values for function parameters. These values will be used if the caller doesn’t provide a value for that parameter:def power(base, exponent=2): return base ** exponent
example of function in python
CALCULATOR WITH IF FUNCTION
def add(x, y): return x + y def subtract(x, y): return x – y def multiply(x, y): return x * y def divide(x, y): if y != 0: return x / y else: return “Cannot divide by zero” print(“Select operation:”) print(“1. Add”) print(“2. Subtract”) print(“3. Multiply”) print(“4. Divide”) choice = input(“Enter choice (1/2/3/4): “) num1 = float(input(“Enter first number: “)) num2 = float(input(“Enter second number: “)) if choice == ‘1’: print(“Result:”, add(num1, num2)) elif choice == ‘2’: print(“Result:”, subtract(num1, num2)) elif choice == ‘3’: print(“Result:”, multiply(num1, num2)) elif choice == ‘4’: print(“Result:”, divide(num1, num2)) else: print(“Invalid Input”)
To-Do List Program:
tasks = [] def add_task(task): tasks.append(task) print("Task added:", task) def remove_task(task): if task in tasks: tasks.remove(task) print("Task removed:", task) else: print("Task not found") def display_tasks(): print("Tasks:") for index, task in enumerate(tasks, start=1): print(f"{index}. {task}") while True: print("Select operation:") print("1. Add Task") print("2. Remove Task") print("3. Display Tasks") print("4. Quit") choice = input("Enter choice (1/2/3/4): ") if choice == '1': task = input("Enter task: ") add_task(task) elif choice == '2': task = input("Enter task to remove: ") remove_task(task) elif choice == '3': display_tasks() elif choice == '4': break else: print("Invalid Input")Function with Default Argument:
def power(x, exponent=2): return x ** exponent result = power(5) # Uses default exponent (2) print(result) result = power(3, 4) # Custom exponent (4) print(result)