Novice Koder


To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it.

<variable name> = <value>

Python uses = to assign values to variables. There's no need to declare a variable in advance (or to assign a datatype to it), assigning a value to a variable itself declares and initializes the variable with that value. There's no way todeclare a variable without assigning it an initial value.

# Integer
a =2
print(a)

# Output: 2
# Integer 
b =9223372036854775807
print(b)

# Output: 
9223372036854775807

# Floating point
pi =3.14
print(pi)

# Output: 3.14

# String
c ='A'
print(c)

# Output: A

# String 
name ='John Doe'

print(name)

# Output: John Doe

# Boolean 
q =True
print(q)

# Output: True

# Empty value or null data type

x =None
print(x)

# Output: None

Variable assignment works from left to right. So the following will give you a syntax error.


0 = x

=> Output: SyntaxError: can't assign to literal

You can not use python's keywords as a valid variable name. You can see the list of keywords by:

import keyword

print(keyword.kwlist)

Rules for variable naming:

1.Variables names must start with a letter or an underscore.

=True    # valid 

_y =True    # valid 

9=False    # starts with numeral

=>SyntaxError: invalid syntax 

$=False# starts with symbol

=>SyntaxError: invalid syntax

2. The remainder of your variable name may consist of letters, numbers and underscores.

has_0_in_it ="Still Valid"

3.Names are case sensitive.

= 9

= X*5

=>NameError: name 'X'isnot defined

Even though there's no need to specify a data type when declaring a variable in Python, while allocating the necessary area in memory for the variable, the Python interpreter automatically picks the most suitable built-in type for it:

= 2

print(type(a))

# Output: <type 'int'>

=9223372036854775807

print(type(b))

# Output: <type 'int'>

pi =3.14

print(type(pi))

# Output: <type 'float'>

='A'

print(type(c))

# Output: <type 'str'>

name ='John Doe'print(type(name))

# Output: <type 'str'>

=True

print(type(q))

# Output: <type 'bool'>

8=None

print(type(x))

# Output: <type 'NoneType'>