Python Variables and Keywords in Python Programming

Python Variables: Like other programming languages, Python also uses the concept of variables. Variables in Python are used to store values to perform the operation in the program. It can store values of any type i.e. Integer, String etc.

Python Keywords: Python keywords are the reserved words that cannot be used in declaring or defining variable. Declaring means just to declare a variable without storing any value to it. Defining means to declare as well as initialize some value to a variable.

Declaration:
x

Definition:
x = 2

Note: In python declaration of an empty variable is not allowed. If we do so, we’ll get an error i.e. x is not defined

We will not discuss variables only. But also discuss some more topics related to variables, We can call it, branches of variables. These are the topics that we discuss:

  • Values and types
  • Variables
  • Variable names and keywords
  • Statements
  • Evaluating expressions

Values and types in python programming

Values and types: Value is one of the fundamental things of any programming language. It may be a letter or maybe a number that the program manipulates. An example of value as a letter/word is “Hello World!” and as a number is 2 etc.
These values belong to different types: 2 is an integer and “Hello World!” is a string. Any value enclosed in quotation marks is taken as a string.

The print statement works on integer also:

//print statement with integer
print(543)
# output
543

We can check the variable types also
Types of Values:

type(2)
type(2.0)
type("Hello World!")

# output
type 'int'
type 'float'
type 'str'

Variables: It has been discussed already. Let’s have a recap, the most powerful and fundamental concept used in any programming language is variables. A variable is a name that refers to a value or a variable is a name that stores some value. It can be of type integer, string or float. Let’s observe with the help of an example:

Example of variables:

a = 24
b = "Hello World!"
print(a)
print(b)

# output
# 24
# Hello World!

Variable and keywords in pythonWe can choose the name of the variable of our choice. But also there are some naming conventions that we have to follow and always need to keep in mind. If we don’t follow these conventions, then definitely we’ll get some error like ‘SyntaxError: invalid syntax’ if we do some error while naming a variable. let’s see some common examples:

name = "python"              # python
74name = "pythonlobby.com"   # SyntaxError
@name = 24                   # SyntaxError

Only first naming conventions is true, second and third one is wrong.

keywords in pythonKeywords: In every programming language there are keywords, these keywords are reserved words whose meaning has already been defined. We cannot use these keywords for naming purposes. Python has a total of 29 reserved words or keywords. Below is the table is given of all the available keywords in python:

list of keywords in python programming

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

 

Statements: A statement is an instruction that a python interpreter executes and produce some result. Till now we have only read about print and assignment (=) statements. The result of the print statement is a value but assignment do not produce a result. It is used to assign value only.

If we execute a statement on the command line, then the code executes and an instant result is displayed if there is one statement only.

>>> x = "Python Lobby"
>>> x
'Python Lobby'

If we execute a complete script that contains a sequence of statements, then complete code executes at once and display the complete result. Let’s check one example:

print(1)
x = 2
print(x)

# output
# 1
# 2

Note: Again the assignment(=) statement produces no output
Evaluating expressions: An expression is the combination of values, statements, operators and keywords. When we run these combinations on IDE or interpreter it produces the result. Example:

# Example on evaluating expressions
message = "Hello World!"
print(message)
# output
# Hello World!