In the field of data science, we often talk about different types of data. Understanding these types is crucial for proper data analysis and interpretation. Here are some of the most common types of data:
Numerical Data: This type of data is quantitative and can be divided into two types: discrete and continuous. Discrete numerical data is countable (e.g., the number of employees in a company), while continuous numerical data can take any value within a range (e.g., the weight of a person).
Categorical Data: This type of data is qualitative and describes categories or groups (e.g., the color of a car). It can be further divided into nominal data (no order implied) and ordinal data (order is significant).
Binary Data: A special case of categorical data where the variable has only two categories or groups (e.g., yes/no, true/false).
Time-series Data: This type of data is collected over time and the order of the data points is important (e.g., stock prices over time).
Text Data: This type of data is unstructured and is typically in the form of sentences or paragraphs (e.g., customer reviews).
In Python, we have several data types that are commonly used. Here are some of them with examples:
Integers: These are whole numbers. For example, 5, 10, -1.
Floats: These are real numbers (i.e., numbers that can have a decimal point). For example, 5.0, 1.23, -0.5.
Strings: These are sequences of characters. For example, 'Hello, World!', 'Python'.
Booleans: These represent truth values True and False.
Lists: These are ordered collections of items (which can be of different types). For example, [1, 2, 3], ['apple', 'banana', 'cherry'].
Tuples: These are like lists, but they are immutable (i.e., they can’t be modified after they are created). For example, (1, 2, 3), ('apple', 'banana', 'cherry').
Dictionaries: These are collections of key-value pairs. For example, {'name': 'John', 'age': 30}.
Python has a variety of operators that can be used with different data types. Here are some examples:
Arithmetic Operators: These are used with numeric values to perform common mathematical operations.
Addition: 5 + 3 returns 8
Subtraction: 5 - 3 returns 2
Multiplication: 5 * 3 returns 15
Division: 5 / 3 returns 1.6666666666666667
Modulus: 5 % 3 returns 2
Exponentiation: 5 ** 3 returns 125
Floor division: 5 // 3 returns 1
Comparison Operators: These are used to compare two values.
Equal: 5 == 3 returns False
Not equal: 5 != 3 returns True
Greater than: 5 > 3 returns True
Less than: 5 < 3 returns False
Greater than or equal to: 5 >= 3 returns True
Less than or equal to: 5 <= 3 returns False
Logical Operators: These are used to combine conditional statements.
and: Returns True if both statements are true
or: Returns True if one of the statements is true
not: Reverse the result, returns False if the result is true
Membership Operators: These are used to test if a sequence is presented in an object.
in: Returns True if a sequence with the specified value is present in the object
not in: Returns True if a sequence with the specified value is not present in the object
String Operators: Python also has a set of operators that are specifically designed to work with strings. These include the + operator for concatenation and the * operator for repetition.
Concatenation: Hello World
Repetition: HelloHelloHello
Slicing: ell
Length: 5
# Exercise 3: Working with listslist1 = [1, 2, 3, 4, 5]# Accessing elementsprint('First element:', list1[0])print('Last element:', list1[-1])# List slicingprint('Slicing:', list1[1:4])# List lengthprint('Length:', len(list1))# Adding elementslist1.append(6)print('After adding an element:', list1)# Removing elementslist1.remove(1)print('After removing an element:', list1)
First element: 1
Last element: 5
Slicing: [2, 3, 4]
Length: 5
After adding an element: [1, 2, 3, 4, 5, 6]
After removing an element: [2, 3, 4, 5, 6]
# Exercise 4: Working with dictionariesdict1 = {'name': 'John', 'age': 30, 'city': 'New York'}# Accessing elementsprint('Name:', dict1['name'])print('Age:', dict1['age'])# Adding elementsdict1['job'] ='Engineer'print('After adding an element:', dict1)# Removing elementsdel dict1['age']print('After removing an element:', dict1)
Name: John
Age: 30
After adding an element: {'name': 'John', 'age': 30, 'city': 'New York', 'job': 'Engineer'}
After removing an element: {'name': 'John', 'city': 'New York', 'job': 'Engineer'}
Python Functions
In Python, a function is a block of reusable code that performs a specific task. Functions help break our program into smaller and modular chunks, making it organized and manageable. They also prevent repetition and make the code reusable.
The def keyword is a statement for defining a function in Python.
function_name is a unique identifier for the function.
parameters (arguments) through which we pass values to a function. These are optional.
A colon (:) to mark the end of the function header.
Optional documentation string (docstring) to describe what the function does.
One or more valid python statements that make up the function body. Statements must have the same indentation level (usually 4 spaces).
An optional return statement to return a value from the function.
# Example of a Python functiondef greet(name):""" This function greets the person passed in as a parameter """print(f'Hello, {name}. Good morning!')# Calling the functiongreet('John')
Hello, John. Good morning!
# Example of a Python function with a return statementdef add_numbers(num1, num2):""" This function adds the two numbers passed in as parameters """return num1 + num2# Calling the function and printing the resultresult = add_numbers(5, 3)print(f'The sum is {result}')
The sum is 8
Exercises
Exercise 1: Data Types
Given the following variables, convert each one to a different data type:
num ='123'str_num =456
Convert num to an integer and str_num to a string. Print the new variables and their types.
Exercise 2: Functions
Write a function named calculate_average that takes a list of numbers as a parameter and returns their average.
Then, call your function with the list [1, 2, 3, 4, 5] and print the result.
Write a function named get_ages that takes the list as a parameter and returns a list of all ages. Then, call your function with the data list and print the result.