Introduction To Python Data Types
In python, every value is considered as an object but all have their data types as well, which will fall in one of the below categories:
- Strings: Set of characters for e.g., ‘I love Python’.
- Numbers: It could be int, float, or a complex number.
- Lists: Compound data types, can be thought of as arrays in C.
- Tuples: More or less like arrays but are immutable.
- Boolean: True or False.
- Set: This data type represents a set of unique values.
- Dictionary: It is like a hash table that has a key and its value/ values.
Key Takeaways
- Python is a high-level, object-oriented, and interpreted programming language.
- Though python considers each value as an object but different kinds of data types available in Python such as Numbers, Strings, Boolean, lists, tuples, sets, and dictionary.
- Discussed basics of practical programming, and how variable assignment plays an important role in writing code.
- We wrote code for interest calculation using simple interest rate formulae. We also wrote some code using the python function.
What Is Python?
- Python is a high-level, object-oriented, and interpreted programming language.
- Python supports dynamic semantics as well.
- It combines dynamic binding and dynamic typing, which makes it very lucrative for the developers for rapid development and scripting. Python has founded by Guido Van Rossum in 1991 (It recently completed 30 years).
Practical Python Programming
- In practical scenarios we do not use these data types directly we assign it to a variable and then use it in programming. Practical programming is dynamic in nature and all these data types are used in programming, assigning variables to make code reusable and easily modifiable.
- In practical cases, we deal with data and apply different logic to accomplish a specific task. In some cases, we need input from the user so we can limit the user to what type of input we want.
What Are Python Programs?
- Programs written in python language are python programs.
- Variable assignment plays an important role in programming.
Examples To Implement Python Data Types
Given below are the examples mentioned:
Example #1
We know simple interest formulae Interest = Principle * interest rate * time let’s implement a program for it.
Let’s say you took INR 50000 on loan at a simple interest of 7.5 % annually for 1 year, How much you would have to pay back at the end of the year.
Code:
P = int(input()) ## Assigning a variable to take Principal amount as input in int
IR = float(input()) ## Assigning variable to take interest rate as input in float
T = int(input()) ## Assigning time variable to take number of years as input
Total_interest = P * IR * T / 100
print(“Simple Interest:”, Total_interest)
The above example is a simple python program to calculate interest.
Output:
Example #2
The beginner shall start writing code with functions the above-written code can be implemented as a function which then can be used anywhere.
Let’s see how to build a function with the above code.
Code:
def calculate_simple_interest(P,IR,T):
print("Principal amount is :", P)
print("Interest rate is :", IR)
print("Time in years is :", T)
Total_interest = P * IR * T / 100
print("Total Interest is : ", Total_interest)
return Total_interest
P = 50000
IR = 7.5
T = 1
calculate_simple_interest(P,IR,T)
Output:
Conclusion – Python Data Types
Thus we saw Python Data Types in detail.
Recommended Articles
This is a guide to Python Data Types. Here we discuss the introduction to python data types, its definition along with its examples and code implementation. You can also go through our other suggested articles to learn more –
- Is Python Object Oriented?
- Pointers in Python
- Python Programming Tips
- Arithmetic Operators in Python
Are you preparing for the entrance exam ?
Join our Python test series to get more practice in your preparation
View More