Variables in Python

Variables are placeholders for important values. Programmers generally choose names for their variables that are meaningful. Variable names can be arbitrarily long. They can contain both letters and numbers, but they have to begin with a letter. It is legal to use uppercase letters, but it is a good idea to begin variable names with a lowercase letter. The underscore character (_) can appear in a name. It is often used in names with multiple words, such as my_name. You will get a syntax error if you use a multiple words variable name without the underscore character. For example, I will get a syntax error if I use ‘my name’ instead of ‘my_name’ as a variable name.

Python has reserved 31 keywords which are given below:

Python_Keywords


The python interpreter uses these keywords to recognize the structure of the program, and they cannot be used as variable names. If you give a variable an illegal name, you get a syntax error.  In Python, equal sign (=) is used assign a variable name and remember that double equals == is used to test equality. A statement is a unit of code that the Python interpreter can execute. For example below is a print statement.


my_name = "Naveed Afzal"
print my_name

Naveed Afzal

A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.
For example, the script

print 1
x = 2
print x

produces the output

1
2

 

Comments

  1. By Smith

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.