Day 10: Basic Python Programming for Data Science
JIGNESH KUMAR
MIS & Admin | Placement Representative | Data Science Enthusiast | ICE 24' SLIET
A. Python Programming Concepts:
1. Basic Syntax:
a) Variables and Data Types:
i) Explanation: Variables are used to store and manage data in a program. Python supports various data types such as integers, floats, strings, lists, tuples, and more.
ii) Coding Example:
# Variable and Data Types
name = "Jignesh"
age = 22
height = 5.9
is_student = True
iii) Use Scenario: Variables and data types are fundamental for storing information in a program. They are used in every Python script or application.
iv) Applications and Benefits:
b) Basic Operators:
i) Explanation: Basic operators in Python include arithmetic operators (+, -, *, /), comparison operators (==, !=, <, >), logical operators (and, or, not), and more.
ii) Coding Example:
# Basic Operators
x = 10
y = 5
# Arithmetic operators
sum_result = x + y
difference = x - y
product = x * y
quotient = x / y
# Comparison operators
is_equal = x == y
is_not_equal = x != y
iii) Use Scenario: Operators are essential for performing calculations and making decisions based on conditions in a program.
iv) Applications and Benefits:
2. Control Flow:
a) Conditional Statements (if, elif, else):
i) Explanation: Conditional statements allow the execution of different code blocks based on specified conditions.
ii) Coding Example:
# Conditional Statements
age = 18
if age < 18:
print("You are a minor.")
elif age >= 18 and age < 21:
print("You are an adult but not allowed to drink.")
else:
print("You are a legal adult.")
iii) Use Scenario: Conditional statements are used to control the flow of a program based on specific conditions.
iv) Applications and Benefits:
b) Loops (for, while):
i) Explanation: Loops in Python enable the repetition of a block of code, either a fixed number of times (for loop) or until a condition is met (while loop).
ii) Coding Example:
# Loops
# For loop
for i in range(5):
print(i)
# While loop
count = 0
while count < 5:
print(count)
count += 1
iii) Use Scenario: Loops are used when you need to iterate over a sequence of elements or perform a task repeatedly until a certain condition is met.
iv) Applications and Benefits:
3. Data Structures:
a) Lists, Tuples, and Sets:
a.I) Lists:
i) Explanation: Lists are ordered, mutable sequences in Python, allowing the storage of multiple values.
ii) Coding Example:
# Lists
fruits = ['apple', 'banana', 'orange']
numbers = [1, 2, 3, 4, 5]
mixed_list = ['apple', 3, True]
# Accessing elements
print(fruits[0]) # Output: 'apple'
# Modifying elements
fruits.append('grape') # ['apple', 'banana', 'orange', 'grape']
iii) Use Scenario: Lists are suitable for situations where you need an ordered and mutable collection of items.
iv) Applications and Benefits:
a.II) Tuples:
i) Explanation: Tuples are ordered, immutable sequences in Python.
领英推荐
ii) Coding Example:
# Tuples
coordinates = (3, 4)
colors = ('red', 'green', 'blue')
# Accessing elements
print(coordinates[0]) # Output: 3
# Immutable - cannot modify elements
# coordinates[0] = 5 # Raises an error
iii) Use Scenario: Tuples are useful when you want to create a collection of values that should remain constant throughout the program.
iv) Applications and Benefits:
a.III) Sets:
i) Explanation: Sets are unordered collections of unique elements.
ii) Coding Example:
# Sets
fruits_set = {'apple', 'banana', 'orange'}
# Adding and removing elements
fruits_set.add('grape')
fruits_set.remove('banana') # {'apple', 'orange', 'grape'}
iii) Use Scenario: Sets are beneficial when you need to work with unique elements and perform set operations.
iv) Applications and Benefits:
b) Dictionaries:
i) Explanation: Dictionaries are unordered collections of key-value pairs.
ii) Coding Example:
# Dictionaries
person = {'name': 'Jignesh', 'age': 22, 'city': 'Buxar'}
# Accessing values
print(person['name']) # Output: 'Jignesh'
# Modifying values
person['age'] = 23 # {'name': 'Jignesh', 'age': 23, 'city': 'Buxar'}
iii) Use Scenario: Dictionaries are useful when you want to associate values with keys for easy retrieval.
iv) Applications and Benefits:
c) String Manipulation:
i) Explanation: String manipulation involves various operations on strings, such as concatenation, slicing, and formatting.
ii) Coding Example:
# String Manipulation
name = 'Jignesh'
greeting = 'Hello, ' + name + '!'
# Slicing
substring = greeting[7:11] # Output: 'Jignesh'
# Formatting
formatted_greeting = f"Hi, {name}!"
iii) Use Scenario: String manipulation is crucial for dealing with textual data and creating formatted outputs.
iv) Applications and Benefits:
4. Functions:
a) Defining Functions:
i) Explanation: Functions allow you to group code into reusable blocks, enhancing modularity and readability.
ii) Coding Example:
# Defining Functions
def greet(name):
return f"Hello, {name}!"
# Calling the function
result = greet('Ramu') # Output: 'Hello, Ramu!'
iii) Use Scenario: Functions are used to encapsulate logic and perform specific tasks, promoting code reusability.
iv) Applications and Benefits:
b) Function Parameters and Return Values:
i) Explanation: Functions can take parameters as input and return values as output.
ii) Coding Example:
# Function Parameters and Return Values
def add_numbers(x, y):
return x + y
# Calling the function with parameters
sum_result = add_numbers(3, 6) # Output: 9
iii) Use Scenario: Functions with parameters and return values enable dynamic and modular code.
iv) Applications and Benefits: