Introduction to Python#
Objective: Understand the basics of Python programming and its application in data analysis.
Python is a high-level programming language renowned for its extensive libraries that greatly facilitate data analysis tasks. To execute code in this notebook, select each cell and press SHIFT-ENTER.
Key Features of Python:
Dynamic Typing: Unlike Java or C++, Python does not require explicit type declarations.
Indentation-based Structure: Code blocks are structured through indentation, not curly braces
{}
.Comments: Use
#
for single-line comments and triple quotes'''
for multi-line comments or documentation.Reference-based Variable Assignment: Variables and function arguments in Python are references, influencing the original objects.
Null Value Representation:
None
signifies a null value. Python does not require semicolons to terminate statements.Importing Modules: External Python files can be integrated using the
import
command, akin to Java’s import or C’s include.
Elementary Data Types#
Python’s standard library supports various elementary data types essential for programming. These include integers, booleans, floating points, and strings. Below is a summary of these data types with examples:
|
Data Type |
Example |
---|---|---|
Number |
Integer |
x = 4 |
|
Floating point |
x = 3.14159 |
|
Boolean |
x = True |
Text |
String |
x = “this” or x = ‘this’ |
num = 10 # Integer
print(num, type(num))
flag = False # Boolean
print(flag, type(flag))
flt = 4.56 # Floating point
print(flt, type(flt))
txt = "Hello, Python!" # String
print(txt, type(txt))
10 <class 'int'>
False <class 'bool'>
4.56 <class 'float'>
Hello, Python! <class 'str'>
The following examples illustrate various arithmetic operations on integers and floating-point numbers in Python
a = 5 # Integer
a1 = a + 3 # Addition
a2 = a * 2 # Multiplication
a += 4 # Equivalent to a = a + 4
a3 = a
a *= 2 # Equivalent to a = a * 2
a4 = a
a5 = a % 3 # Modulo (remainder) operator
b = 2.5 # Floating point number
b1 = b - 1 # Subtraction
b2 = b / 2 # Division
b3 = b // 2 # Integer division
b4 = b ** 2 # Square of b
b5 = b4 ** 0.5 # Square root
b6 = pow(b,2) # Equivalent to square of b
b7 = round(b) # Rounding b to its nearest integer
b8 = int(b) # Type casting float to int
print(a,a1,a2,a3,a4,a5)
print(b,b1,b2,b3,b4)
print(b5,b6,b7,b8)
18 8 10 9 18 0
2.5 1.5 1.25 1.0 6.25
2.5 6.25 2 2
The following examples demonstrate a range of mathematical operations like square root, power, exponential, logarithmic, trigonometric, and other functions available in the math module, as well as special cases like NaN (Not a Number) and infinity.
import math
a = 9
print(math.sqrt(a)) # sqrt(9) = 3
print(math.pow(a, 2)) # 9**2 = 81
print(math.exp(a)) # exp(9)
print(math.log(a, 3)) # log base 3
print(math.fabs(-9)) # absolute value
print(math.factorial(a)) # 9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1
b = 0.5
print(math.ceil(b)) # ceiling function
print(math.floor(b)) # floor function
print(math.trunc(b)) # truncate function
b = 2 * math.pi # math.pi = 3.141592653589793
print(math.sin(b)) # sine function
print(math.tanh(b)) # hyperbolic tangent function
a = math.nan # not a number
print(math.isnan(a))
a = math.inf # infinity
print(math.isinf(a))
3.0
81.0
8103.083927575384
2.0
9.0
362880
1
0
0
-2.4492935982947064e-16
0.9999930253396107
True
True
The following examples demonstrate how to use basic logical operators like AND, OR, and NOT with boolean values.
a = True
b = False
print(a and b) # logical AND
print(a or b) # logical OR
print(a and not b) # combining AND and NOT
False
True
True
The following examples illustrate various string manipulation techniques in Python, such as slicing, length calculation, case conversion, splitting, replacing, finding substrings, comparison, concatenation, and replication.
str1 = "Hello"
print(str1[1:]) # Print all characters except the first
print(len(str1)) # Get the string length
print("Length of string is " + str(len(str1))) # Type casting int to str
print(str1.upper()) # Convert to upper case
print(str1.lower()) # Convert to lower case
str2 = "Hello World"
words = str2.split(' ') # Split the string into words
print(words[1])
print(str2.replace('World','Everyone')) # Replace "World" with "Everyone"
print(str2.find("W")) # Find the position of "W" in str2
print(str1 in str2) # Check if str1 is a substring of str2
print(str1 == 'Hello') # Equality comparison
print(str1 < 'Help') # Inequality comparison
print(str2 + " from Python") # String concatenation
print((str1 + " ")* 3) # Replicate the string 3 times
ello
5
Length of string is 5
HELLO
hello
World
Hello Everyone
6
True
True
True
Hello World from Python
Hello Hello Hello
Compound Data Types#
The following examples demonstrate various list operations in Python such as accessing elements, slicing, appending, popping, concatenating, replicating, sorting, and joining elements into a string.
numList = [2, 4, 6, 8, 10]
print(type(numList))
print(numList)
numList2 = list(range(1, 10, 2)) # range[startvalue, endvalue, stepsize]
print(numList2)
print(numList[2]) # Get the third element of the list
print(numList[:2]) # Get the first two elements
print(numList[2:]) # Get the last three elements
print(len(numList)) # Number of elements in the list
print(sum(numList)) # Sum of elements in the list
numList.append(12) # Append 12 to end of the list
print(numList)
print(numList.pop()) # Remove last element
print(numList)
print(numList + [14, 16, 18]) # Concatenate two lists
print(numList * 2) # Replicate the list
numList.insert(1, 3) # Insert item 3 at index 1
print(numList)
numList.sort(reverse=True) # Sort in descending order
print(numList)
wordList = ['hello', 'world', 'in', 'a', 'list']
print(wordList)
print(type(wordList))
print("list" in wordList) # Check if "list" is in wordList
print(wordList[2]) # Show the 3rd element
print(wordList[:2]) # Show the first two elements
print(wordList[2:]) # Show the last two elements
wordList.append("indeed") # Append element to end
separator = " "
print(separator.join(wordList)) # Merge elements into a string
wordList.remove("world") # Remove element
print(wordList)
<class 'list'>
[2, 4, 6, 8, 10]
[1, 3, 5, 7, 9]
6
[2, 4]
[6, 8, 10]
5
30
[2, 4, 6, 8, 10, 12]
12
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10, 14, 16, 18]
[2, 4, 6, 8, 10, 2, 4, 6, 8, 10]
[2, 3, 4, 6, 8, 10]
[10, 8, 6, 4, 3, 2]
['hello', 'world', 'in', 'a', 'list']
<class 'list'>
True
in
['hello', 'world']
['in', 'a', 'list']
hello world in a list indeed
['hello', 'in', 'a', 'list', 'indeed']
The following examples illustrate various dictionary operations in Python, such as adding key-value pairs, accessing keys and values, checking for the presence of keys, zipping two lists into a dictionary, and sorting a dictionary by keys or values.
bookRatings = {}
bookRatings['1984'] = 4.5
bookRatings['To Kill a Mockingbird'] = 4.8
bookRatings['The Great Gatsby'] = 4.7
bookRatings['Pride and Prejudice'] = 4.6
print(bookRatings)
print(bookRatings.keys()) # Get the keys of the dictionary
print(bookRatings.values()) # Get the values of the dictionary
print(len(bookRatings)) # Get number of key-value pairs
print(bookRatings.get('1984'))
print("Moby Dick" in bookRatings)
print("The Great Gatsby" in bookRatings)
genres = ['Sci-Fi', 'Drama', 'Fiction', 'Romance']
books = ['Dune', 'Hamlet', '1984', 'Pride and Prejudice']
genreDict = dict(zip(genres, books))
print(genreDict)
print(sorted(genreDict)) # Sort keys of dictionary
print(sorted(genreDict.items(), key=lambda item: item[0])) # Sort by key
print(sorted(genreDict.items(), key=lambda item: item[1])) # Sort by value
{'1984': 4.5, 'To Kill a Mockingbird': 4.8, 'The Great Gatsby': 4.7, 'Pride and Prejudice': 4.6}
dict_keys(['1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Pride and Prejudice'])
dict_values([4.5, 4.8, 4.7, 4.6])
4
4.5
False
True
{'Sci-Fi': 'Dune', 'Drama': 'Hamlet', 'Fiction': '1984', 'Romance': 'Pride and Prejudice'}
['Drama', 'Fiction', 'Romance', 'Sci-Fi']
[('Drama', 'Hamlet'), ('Fiction', '1984'), ('Romance', 'Pride and Prejudice'), ('Sci-Fi', 'Dune')]
[('Fiction', '1984'), ('Sci-Fi', 'Dune'), ('Drama', 'Hamlet'), ('Romance', 'Pride and Prejudice')]
The examples below demonstrate the creation and use of tuple objects. Tuples, distinct from lists, are immutable, meaning they can’t be altered once they’re created.
AnimalTuple = ('Dog', 'Canine', 'Bark')
BirdTuple = ('Bird', 'Avian', 'Chirp')
FishTuple = ('Fish', 'Aquatic', 'Swim')
print(AnimalTuple)
print(AnimalTuple[1:])
creatures = [AnimalTuple, BirdTuple, FishTuple] # This will create a list of tuples
print(creatures)
print(creatures[1])
print(creatures[1][:])
print(creatures[1][1:])
creatures.sort(key=lambda creature: creature[2]) # Sort the creatures by their third attribute
print(creatures)
('Dog', 'Canine', 'Bark')
('Canine', 'Bark')
[('Dog', 'Canine', 'Bark'), ('Bird', 'Avian', 'Chirp'), ('Fish', 'Aquatic', 'Swim')]
('Bird', 'Avian', 'Chirp')
('Bird', 'Avian', 'Chirp')
('Avian', 'Chirp')
[('Dog', 'Canine', 'Bark'), ('Bird', 'Avian', 'Chirp'), ('Fish', 'Aquatic', 'Swim')]
Control Flow Statements#
The following examples illustrate the use of if-else statements, for loops with lists, tuples, dictionaries, and a while loop in Python. Each example demonstrates a common use case for these control flow statements.
print("========== Using if-else statement ==========")
x = 7
if x % 3 == 0:
print("x =", x, "is divisible by 3")
else:
print("x =", x, "is not divisible by 3")
if x > 5:
print("x =", x, "is greater than 5")
elif x < 5:
print("x =", x, "is less than 5")
else:
print("x =", x, "is 5")
print("========== Using For Loop with a List ==========")
fruits = ['apple', 'banana', 'cherry']
for fruit in fruits:
print(fruit.upper())
fruit_lengths = [len(fruit) for fruit in fruits]
print(fruit_lengths)
print("========== Using For Loop with List of Tuples ==========")
cities = [('New York', 'NY'), ('Los Angeles', 'CA'), ('Chicago', 'IL')]
sorted_states = [city[1] for city in cities]
sorted_states.sort()
print(sorted_states)
print("========== Using For Loop with Dictionary ==========")
colors = {'red': 3, 'blue': 4, 'green': 2}
color_names = [k for (k, v) in colors.items()]
print(color_names)
print("========== Using While Loop ==========")
numbers = list(range(-3, 3))
print(numbers)
i = 0
while numbers[i] < 0:
i += 1
print("First non-negative number:", numbers[i])
========== Using if-else statement ==========
x = 7 is not divisible by 3
x = 7 is greater than 5
========== Using For Loop with a List ==========
APPLE
BANANA
CHERRY
[5, 6, 6]
========== Using For Loop with List of Tuples ==========
['CA', 'IL', 'NY']
========== Using For Loop with Dictionary ==========
['red', 'blue', 'green']
========== Using While Loop ==========
[-3, -2, -1, 0, 1, 2]
First non-negative number: 0
Functions#
The following examples, the lambda function creates a simple unnamed function for a quadratic equation, and the filter_odds function demonstrates a more complex operation with an optional sorting feature.
# Example of an unnamed function using lambda
square_func = lambda x: x**2 + 5*x - 6
print(square_func(3))
import math
# Function to filter out odd numbers and optionally sort the list
def filter_odds(inlist, sortFlag=False):
outlist = [item for item in inlist if item % 2 == 0]
if sortFlag:
outlist.sort()
return outlist
mylist = [3, 4, 2, 7, 6, 9]
print(filter_odds(mylist, True))
18
[2, 4, 6]
File I/O#
In this example, a list of tuples containing state abbreviations, state names, and their capitals is written to a file and then read from the file. The read data is processed and printed in a formatted manner.
# Data to be written to a file
books = [('1984', 'George Orwell', '1949'), ('To Kill a Mockingbird', 'Harper Lee', '1960'),
('The Great Gatsby', 'F. Scott Fitzgerald', '1925'), ('Pride and Prejudice', 'Jane Austen', '1813')]
# Writing data to a file
with open('books.txt', 'w') as file:
for book in books:
file.write(','.join(book) + '\n')
# Reading data from the file and processing it
with open('books.txt', 'r') as file:
for line in file:
fields = line.strip().split(',')
print(f"Book: {fields[0]} - Author: {fields[1]} - Year: {fields[2]}")
Book: 1984 - Author: George Orwell - Year: 1949
Book: To Kill a Mockingbird - Author: Harper Lee - Year: 1960
Book: The Great Gatsby - Author: F. Scott Fitzgerald - Year: 1925
Book: Pride and Prejudice - Author: Jane Austen - Year: 1813