Performing data analysis requires a proper understanding of how the tool you are using works with data.
Python is a valuable tool for data analysis, but before you jump the gun and load up your datasets, let's explore how Python handles data.
Python Data Handling
Data in Python is usually stored in a variable. A variable is a named location reserved to store values in memory. In order to store any data in a variable, one must name it. A variable is defined using an equals (=) sign.
Syntax of a Variable
variable_name = data
Variable Naming Conventions
A variable name must follow these rules:
- The name can be composed of uppercase, lowercase, and/or the underscore character (
_). - The variable name must begin with a character.
- The underscore is considered a letter when used in a variable name.
- The variable name is case sensitive.
- The variable name must not be a Python reserved keyword.
Reserved keywords have their meaning predefined and thus cannot be used to name variables. You can view the reserved keywords by running the following code:
import keyword
print("The list of keywords are : ")
print(keyword.kwlist)
Variables make code reusable since data is only assigned once, the data can be accessed anywhere in the code by calling the variable rather than rewriting it, and updating the variable updates the data in all instances where it has been used in the code.
A Python variable can only hold data of one type at a time. Thus, it is important to have a quick overview of Python data types.
Python Data Types
| Category | Data Types | Description |
|---|---|---|
| Text Type | str |
Used to store data in text format. |
| Numeric Types |
int, float, complex
|
Used to store numeric values. |
| Sequence Types |
list, tuple, range
|
Used to store an ordered collection of items, which can be accessed by indexing. |
| Mapping Type | dict |
Used to store data in key-value pairs where each key in a dictionary must be unique. Values are accessed through the key. |
| Set Types |
set, frozenset
|
Used to store data in an unordered collection where each data item must be unique. |
| Boolean Type | bool |
Used to store boolean values True or False. |
| None Type | NoneType |
Used to assign a variable with no value assigned to it. |
Data can be assigned to a variable manually in the code, or more usually, through user input. This is done using the input() function.
Input() function
The input() function collects the user input and directly assigns that input to a variable.
Syntax of the input() Function
variable_name = input("input message")
For example:
name = input("Enter your name : ")
In this instance, the user will be prompted to enter their name, and their input is then assigned to the variable name.
If we need to also know the user's age, we can create another variable and assign it the user's input using the input() function.
age = input("Enter your age : ")
However, at this point it is important to note that the input() method, by default, collects data in the string data type. Thus, even if our user enters numeric data for their age, it will be stored as a string in the age variable.
You can confirm this by running the following code to check the data type of our two variables using the type() function.
print(type(name))
print(type(age))
You should get the output:
class 'str'
Indicating both variables are of the data type string.
Do not despair, this can easily be mitigated using a Python technique called type casting.
Type casting
Type casting is the process of converting a Python variable from one data type to another. This can be done when the variable is being defined, or at any point once a variable is defined.
I can illustrate this by changing the data type of our age variable to an integer as follows:
age = input("Enter your age : ")
age = int(age)
print(type(age))
Now when we check the data type of the age variable, we get the following output:
class 'int'
This shows that we have successfully changed the data type of our variable.
However, it is more efficient to perform type casting while defining the variable. This is done by wrapping the input() function in the data type function we want our variable to store.
In our case, we can assign the age variable as an integer as follows:
age = int(input("Please enter your age : "))
print(type(age))
Data Output in Python
In order to view the data stored in our variables, it is important to understand the print() function. I have already provided a few examples on what it is used for, when viewing the data types of our variables and viewing the Python reserved keywords, but let's delve into its functionality a bit further.
The print() function displays data passed to it to the standard output of the device, usually the console.
This data can be a variable, where it will display the data in the variable. For example, if we run the following code:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(name)
print(age)
Once the user enters the requested data, it is printed to the console.
We can combine the two print statements into one to make a more cohesive output using f-strings.
An f-string (formatted string literal) is a Python functionality that enables one to embed variables and expressions directly inside a string by prefixing the string with f and using curly braces.
We can rewrite our code as follows:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}! You are {age} years old.")
In this case, we have a single output that displays data from both of our variables.
Conclusion
Mastering the foundational building blocks of handling data in Python, which are essential first steps before doing any data analysis.
Almost every analysis task starts with getting data into the right shape.
This will often require understanding variables and their data types to prevents subtle bugs (e.g., trying to do math on a string). Type casting lets you turn messy, string-based input into usable numeric data. And clear output formatting (via print()/f-strings) is how you inspect, validate, and communicate results as you build up more complex analysis code.
Top comments (0)