Day 5: Data Types, Variables, and Type Conversion in Python
Welcome to Day 5 of our "Master Python in 100 Days" journey! Today, we will explore some of the most important building blocks in Python: data types, variables, and type conversion. Understanding these concepts is essential as they form the basis of how we store and manipulate data in our programs.
Data types tell Python what kind of data we are working with. Python has several built-in data types, and the most common ones are:
1. Integers (int): These are whole numbers without decimal points, like 10
, -5
, 0
, etc.
2. Floats (float): These are numbers with decimals, like 3.14
, 0.001
, -2.5
.
3.
Strings (str): A string is a collection of characters (letters, numbers, symbols) enclosed in quotes (" "
or ' '
). These are used to represent text.
4. Booleans (bool): These are either True
or False
values. It’s used for logical conditions.
Checking the Data Type
You can check the data type of any variable using the type()
function:
Explanation:
x = 50
:- Here, you create a variable named
x
and store the value50
in it. Since50
is a whole number without a decimal point, Python automatically considers it an integer. - When you use the
type()
function and passx
as an argument (i.e.,type(x)
), Python checks what type of data is stored inx
. - The output is
<class 'int'>
, which meansx
is an integer.
- Here, you create a variable named
y = 10.5
:- In this line, you create a variable named
y
and store the value10.5
in it. Since10.5
has a decimal point, Python recognizes it as a float (a floating-point number, which means a number with decimals). - Similarly, using
type(y)
checks the data type ofy
, and the output is<class 'float'>
, which meansy
is a float.
- In this line, you create a variable named
What Are Variables?
Variables are like containers or boxes where we store values. Just like how we put things into boxes with labels to remember what’s inside, we use variables in programming to store data and give them a name.
How to Create Variables in Python?
In Python, making a variable is very easy. You just need to use the =
sign (assignment operator) to store some data in a variable.
Here’s an example:
age
stores the number25
(an integer).name
stores the text"Rahul"
(a string).height
stores the decimal number5.9
(a float).is_student
stores the valueTrue
(a boolean, which is either True or False).
_
) are allowed in variable names._
), not with a number.student_name
, _count
1student
, 3dat
Age
, age
, and AGE
are all different variables in Python.- Explanation :
Case Sensitivity:
- Python is case-sensitive. This means that the variables
age
,Age
, andAGE
are all different because they use different combinations of small and capital letters. - In the code, we create three variables:
age
,Age
, andAGE
. Even though they look similar, Python treats them as different because of the difference in letter cases.
- Python is case-sensitive. This means that the variables
Assigning Values:
age = 25
:- We assign the value
25
to the variableage
(with all small letters).
- We assign the value
Age = 30
:- The variable
Age
(with a capital "A") is assigned the value30
.
- The variable
AGE = 35
:- The variable
AGE
(with all capital letters) is given the value35
.
- The variable
- Python treats these variables (
age
,Age
, andAGE
) as separate from each other, even though their names look similar.
Using
print()
:The
print()
function shows the values of the variables. When Python seesprint(age, Age, AGE)
, it collects the values fromage
,Age
, andAGE
and prints them.Since
age = 25
,Age = 30
, andAGE = 35
, the output will be:
Output:
Type Conversion in Python
Sometimes, we need to change the type of data. For example, you might want to change a number (int or float) into a string, or a string into a number.
This is called type conversion or type casting.
Python provides some built-in functions for this:
- int(): Converts a value into an integer (whole number).
- Example:
int()
: Converts a float value (like 3.14) to an integer by removing the decimal part. So3.14
becomes3
.
- Example:
float()
: Converts an integer (like 10) into a float by adding.0
at the end. So10
becomes10.0.
- Example:
str()
: Converts any value (like the number25
) into a string by wrapping it in quotes. So25
becomes"25"
, and now it's text.
More Examples of Type Conversion
Explanation:
- Convert float to int:
7.89
is converted to7
(it discards the decimal part). - Convert int to float:
10
is converted to10.0
. - Convert int to string: The integer
30
becomes"30"
(as a string). - Convert string to int: The string
"100"
is converted to100
(as an integer).
Important: You can only convert a string to an integer or float if the string is a number. If the string has letters, it will throw an error. Example:
Key Points
Variables : These are used to store data and can be thought of as named containers that hold different types of information.
Data Types : Python has various data types, including integers (whole numbers), floats (decimal numbers), strings (text), and booleans (True or False). Each type helps in organizing and storing different kinds of data.
Type Conversion :This feature allows us to change data from one type to another. For example, we can convert a float to an integer or a string to an integer.
Functions for Type Conversion : In Python, we commonly use int()
, float()
, and str()
to convert data types. These functions help us ensure that the data is in the correct format for our needs.
Variables : These are used to store data and can be thought of as named containers that hold different types of information.
Data Types : Python has various data types, including integers (whole numbers), floats (decimal numbers), strings (text), and booleans (True or False). Each type helps in organizing and storing different kinds of data.
Type Conversion :This feature allows us to change data from one type to another. For example, we can convert a float to an integer or a string to an integer.
Functions for Type Conversion : In Python, we commonly use int()
, float()
, and str()
to convert data types. These functions help us ensure that the data is in the correct format for our needs.
Today, we have learned about the power of variables, the importance of data types, and how type conversion helps us make our programs flexible. This is a key lesson because, in every Python program, you will use these concepts again and again.
Keep practicing, and you will get better every day. Tomorrow, we will move on to more advanced topics in Python.
Today, we have learned about the power of variables, the importance of data types, and how type conversion helps us make our programs flexible. This is a key lesson because, in every Python program, you will use these concepts again and again.
Keep practicing, and you will get better every day. Tomorrow, we will move on to more advanced topics in Python.
Comments
Post a Comment