Previous Index Next

Variables and Types

When we create a program, we often like to store values so that it can be used later. Programs use variables to access and manipulate data that is stored in memory. A variable is a name that represents a value in the computer’s memory.

Let's take an example to understand this better. The following program uses variables to hold data:

Program (sum-two-numbers.py)

a = 10
b = 15
c = a + b
print("Sum of numbers are", c)

The above code defines three variables: a, b, and c. The first line sets the value of a to 10, and the second line sets the value of b to 15.

The third line calculates the sum of a and b by using the + operator, and assigns the result to c.

Finally, the print() function is used to display the message "Sum of numbers are" followed by the value of c.

After running the code, the output will be displayed in the console, which should be:

Sum of numbers are 25

Creating Variables

In python, variable is created when we assign value to it. In following code fragment two variables a and b are created.

a = 10
b = 15

Creating Multiple Variables in single statement with different values

x, y, z = "Orange", "Banana", "Apple"

Creating Multiple Variables in single statement with same values

x = y = z = "Orange"

Data Type

In Python, the datatype of a variable is determined dynamically at runtime, based on the value assigned to it. This means that you do not need to declare the datatype of a variable before assigning a value to it. Python automatically assigns the appropriate datatype based on the value you provide.

For example, if you assign an integer value to a variable, Python will assign the datatype "int" to that variable. If you assign a string value, Python will assign the datatype "str" to the variable. Similarly, if you assign a floating-point number, Python will assign the datatype "float" to the variable.

Examples:

x = 10        # int
y = 3.14      # float
z = "Hello"   # str

You can check the datatype of a variable using the type() function. For example:

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

y = 3.14
print(type(y))  # Output: <class 'float'>

z = "Hello"
print(type(z))  # Output: <class 'str'>

In Python, there are several built-in datatypes, including:

datatype

When you perform operations on variables, Python will automatically convert the datatypes if necessary. For example, if you add an integer and a float, Python will convert the integer to a float before performing the addition:

x = 10        # int
y = 3.14      # float
z = x + y     # float
print(z)      # Output: 13.14

In summary, Python dynamically determines the datatype of a variable based on the value assigned to it, and you can check the datatype using the type() function.

Naming Rule of Variable

To name variables in Python, you must follow these rules:

  • A variable name can be of any size
  • A variable name have allowed characters, which are a-z, A-Z, 0-9 and underscore (_)
  • A variable name should begin with an alphabet or underscore
  • A variable name should not be a keyword
  • A variable name cannot contain spaces.
  • Uppercase and lowercase characters are distinct.

Here are some examples of valid and invalid variable names in Python:

Valid variable names:

age = 25
name = "John"
email_address = "john@example.com"
total_sales = 500.50
first_name = "Mary"

Invalid variable names:

2nd_grade = 80    # cannot start with a number
first-name = "John"    # cannot use hyphens
total/sales = 500    # cannot use forward slash
email address = "john@example.com"    # cannot use spaces
if = 10    # cannot use Python keywords as variable names

Keywords

There are some reserved words in Python which have predefined meaning called keywords. These words may not be used as variable name. Some commonly used Keywords are given below:

and del from not while
as elif global or with
assert else if pass yield
break except import print
class exec in raise
continue finally is return
def for lambda try

Previous Index Next