DEV Community

Anurag Verma
Anurag Verma

Posted on

Variables and Data Type in Python

A variable in Python is a name that refers to a value. It gives you the ability to store and manipulate data in your program. To make a variable, simply give it a name and assign it a value with the assignment operator (=). Here's an illustration:

x = 5
Enter fullscreen mode Exit fullscreen mode

In this example, we'll make a variable called x and assign it the value 5. Now, whenever we refer to x in our program, Python will replace it with the value 5.

Variable naming rules:

  • Variable names must start with a letter or underscore (_), followed by any combination of letters, underscores, and digits.

  • Variable names are case sensitive, so x and X are two different variables.

  • There are some reserved keywords in Python that you can't use as variable names, such as if, else, while, and def.

Python has several data types, each of which represents a different type of value. Here are some of the most common Python data types:

  • Integers: Integers (or int for short) represent whole numbers. For example:

    x = 5
    y = -10
    
  • Floating-point numbers: Floating-point numbers (or float) represent numbers with decimal places. For example:

    x = 3.14
    y = -0.5
    
  • Strings: Strings (or str) represent text. They're created by enclosing a sequence of characters in single or double quotes. For example:

    x = 'Hello, world!'
    y = "Python is awesome"
    
  • Booleans: Booleans (or bool) represent True or False values. For example:

    x = True
    y = False
    

There are more complex data types in Python, such as lists, tuples, and dictionaries, but these are the most common ones.

The type() function can be used to determine the data type of a variable. As an example:

x = 5
print(type(x))  # Output: <class 'int'>

y = 'Hello, world!'
print(type(y))  # Output: <class 'str'>
Enter fullscreen mode Exit fullscreen mode

Type conversion:

Python has built-in functions for converting between different data types. For example, you can use the int() function to convert a string to an integer, or the str() function to convert a float to a string. Here are a couple of examples:

x = '5'
y = int(x)  # Convert string to integer
print(y)  # Output: 5

x = 3.14
y = str(x)  # Convert float to string
print(y)  # Output: '3.14'
Enter fullscreen mode Exit fullscreen mode

None type:

Python has a special data type called None. It is similar to null in other programming languages in that it represents the absence of a value. None can be used as a default value for a variable that hasn't yet been assigned a value, or as a placeholder for a value that hasn't yet been computed. Here's an illustration:

x = None
if x is None:
    print("x has no value yet")
else:
    print("x has a value of", x)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)