Skip to main content

Day 5: Understanding Variables and Data Types in Python | Master Python in 100 Days


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.


What are Data Types?

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:

  1. x = 50:

    • Here, you create a variable named x and store the value 50 in it. Since 50 is a whole number without a decimal point, Python automatically considers it an integer.
    • When you use the type() function and pass x as an argument (i.e., type(x)), Python checks what type of data is stored in x.
    • The output is <class 'int'>, which means x is an integer.
  2. y = 10.5:

    • In this line, you create a variable named y and store the value 10.5 in it. Since 10.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 of y, and the output is <class 'float'>, which means y is a float.


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 number 25 (an integer).
  • name stores the text "Rahul" (a string).
  • height stores the decimal number 5.9 (a float).
  • is_student stores the value True (a boolean, which is either True or False).
As you can see, we can store different types of data in variables.



Naming Variables in Python

When you create variables, you need to follow some simple rules:

1. Letters, numbers, and underscores (_) are allowed in variable names.

2. A variable name must start with a letter or underscore (_), not with a number.

  • Correct: student_name, _count
  • Incorrect: 1student, 3dat

  • 3. Variable names are case-sensitive. This means Age, age, and AGE are all different variables in Python.

    For example:
    • Explanation :
    • Case Sensitivity:

      • Python is case-sensitive. This means that the variables age, Age, and AGE are all different because they use different combinations of small and capital letters.
      • In the code, we create three variables: age, Age, and AGE. Even though they look similar, Python treats them as different because of the difference in letter cases.
    • Assigning Values:

      • age = 25:
        • We assign the value 25 to the variable age (with all small letters).
      • Age = 30:
        • The variable Age (with a capital "A") is assigned the value 30.
      • AGE = 35:
        • The variable AGE (with all capital letters) is given the value 35.
      • Python treats these variables (age, Age, and AGE) as separate from each other, even though their names look similar.
    • Using print():

      • The print() function shows the values of the variables. When Python sees print(age, Age, AGE), it collects the values from age, Age, and AGE and prints them.

      • Since age = 25, Age = 30, and AGE = 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:

    1. 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. So 3.14 becomes 3.

        2. float(): Converts a value into a float (decimal number).
    • Example:

    • float(): Converts an integer (like 10) into a float by adding .0 at the end. So 10 becomes 10.0.

    3. str(): Converts a value into a string (text).
    • Example:
    • str(): Converts any value (like the number 25) into a string by wrapping it in quotes. So 25 becomes "25", and now it's text.


    More Examples of Type Conversion

    Explanation:

    1. Convert float to int: 7.89 is converted to 7 (it discards the decimal part).
    2. Convert int to float: 10 is converted to 10.0.
    3. Convert int to string: The integer 30 becomes "30" (as a string).
    4. Convert string to int: The string "100" is converted to 100 (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.


    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

    Popular posts from this blog

    Day 3: Writing Our First Python Program in Detail | Master Python in 100 Days

    Day 3: Writing Our First Python Program in Detail  W elcome back to Master Python in 100 Days ! Today, on Day 3, we are going to write our very first Python program from scratch, step-by-step. This isn’t just about writing code; it’s about understanding every line and making sure we know exactly what’s happening in our program. Let's get started! A Quick Review: What We Did on Day 1 If you remember, on the very first day of the   Master Python in 100 Days , we touched upon how to use the print() function to display basic output on the screen. That was a warm-up , where we gave a quick look at the print() function. But starting from today, we will dive deeper into the actual use of print() . By the end of this session, you’ll not only understand how to use print() properly, but also learn how to avoid common errors and use it in your own Python scripts. So, let’s take our Python learning journey one step further!   Introduction to the print() Function : The ...

    Day 1: Introduction to Python | Master Python in 100 Days

    Day 1: Introduction to Python Hello everyone! Welcome to Day 1 of our 100 Days to Learn Python journey! Today, we will start by understanding what Python is, its importance, and how to get started. What is Python? Python is a popular, high-level programming language that was created by Guido van Rossum and first released in 1991 . It’s designed to be easy to read and simple to write, making it perfect for both beginners and advanced programmers. Python allows developers to focus on solving problems rather than worrying about the complex syntax often found in other programming languages. Why Learn Python? 1. High demand: Many companies need Python programmers. 2. Versatile: You can use Python for web development, data analysis, machine learning, game   development, automation, and more. 3.  Great community: There are many resources, forums, and libraries available online.    Key Features of Python: 1. Simple and Easy to Learn : Python’s syntax is similar to the...