Python – Variables, Values, Types & Keywords.

Variables, Values, Types & Keywords in python


A value can be a number, letter, word etc. It can be of any data type like int, string, float etc.

eg: 100,20.20,”itvoyagers”

>>> a="itvoyagers"
>>> a
>>> b=5
>>> b
>>> c=50.5
>>> c

NOTE : In Python it is not necessary to mention the datatype before defining variable. Value assigned to variable denotes the datatype of that variable

Type function

If you want to know the type of value assigned to a variable , use type statement.

Example: type(a),type(b),type(3),type(“3”) etc


Variables are pointers pointing to a memory location containing value or data. Depending on the data type of value assigned, the interpreter reserves the memory. In python variable declaration is not required. To assign a value to the variable equal to (=) sign is used. Value is present on left side of “=” sign and variable to the right.

eg : p=100

Here , id(p) will show the memory location of data

type(p) will show the datatype of data at which p is pointing

p=q will create a new pointer “q” that will point to the same memory location as that of “p”

Multiple assignments

You can assign same value to many/different variables or different values to different variables in single statement. Also you can use same variable to re-assign different value after using or deleting previous value.

#same value to multiple variables
>>> f=o=g=100
>>> f
>>> o
>>> g
#different values to different variables
>>> m,i,b="itvoyagers",1,1.1 
>>> m
>>> i
>>> b
#reusing same variable to assign new value
>>> i="India"
>>> i
>>> i=1947
>>> i

#Program to swap values
x,y=10,500  #step 1
print("before swap")
print("x is",x)
print("y is",y)
y,x=x,y # step 2
print("after swap")
print("x is",x)
print("y is",y)
NOTE: In above example step 2 is swapping values.Python enables swapping in single line

Rules to define variables name

1. Variables are case sensitive

2. Variables can be alphanumeric

3. Variable name should begin with letters (a-z,A-Z) or Underscore (_)

4. Variable name can never begin with number

5. Keywords can never be used as variable name

6. Variables can be of any length

7. No spaces are allowed in variable name

8. No special symbols are allowed apart from underscore(_)

9. No need to give data type name before defining variable

#few examples of rules
#Variables are case sensitive
>>> a=5
>>> A
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'A' is not defined
#Variable name can never begin with number
SyntaxError: invalid syntax
#Keywords can never be used as variable name
>>> if=5
SyntaxError: invalid syntax
# No spaces are allowed in variable name
>>> first name="pk"
SyntaxError: invalid syntax


Keywords are reserved words and cannot be used as variable names.

There are 33 keywords in python.

Python Keywords


NOTE : All keywords are in lower case except True, False and None have first letter in uppercase

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
New posts coming soon.......

Leave a Comment