Skip to main content

Variables and Keywords


A variable can be used to store a certain value or object. In Python, all numbers (and everything else, including functions) are objects. A variable is created through assignment. Their type is assigned dynamically.
Eg:
x=”binu”
y=23
z=24.5
l=399379379379387983793773973977000102

use type(object) to know the type of the variable

Eg: type(x)     type(y)           type(z)            type(l)
<type 'str'>      <type 'int'>      <type 'float'>   <type 'long'>
When we create a program, we often like to store values so that it can be used later. We
use objects to capture data, which then can be manipulated by computer to provide
information. By now we know that object/ variable is a name which refers to a value. It has a type and also store some value.

Variable can be mutable or immutable. A mutable variable is one whose value may change in place, whereas in an immutable variable change of value will not happen in place.

Programmers choose the names of the variable that are meaningful. A variable name:
1. Can be of any size
2. Have allowed characters, which are a-z, A-Z, 0-9 and underscore (_)
3. should begin with an alphabet or underscore
4. should not be a keyword

It is a good practice to follow these identifier naming conventions:

1. Variable name should be meaningful and short
2. Generally, they are written in lower case letters


Keywords

They are the words used by Python interpreter to recognize the structure of program.
As these words have specific meaning for interpreter, they cannot be used for any other
purpose.
There are 29  keywords in Python 2.7 

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

the following are some invalid variable names
>>> 76trombones = ’big parade’
SyntaxError: invalid syntax
Note: variable name cannot start with a digit.
>>> more$ = 1000000
SyntaxError: invalid syntax
Note:variable name cannot contain $
>>> class = ’Computer Science 101’
SyntaxError: invalid syntax
Note:keyword cannot be used as variable name. 

Remember:

Y Variables are created when they are first assigned a value.
Y Variables type is determined by the value which is assigned.
Y Variables must be assigned a value before using them in expression,
Y Variables refer to an object and are never declared ahead of time.

Comments

Popular posts from this blog

Strings in Python

Strings are compound data type made of sequence of characters. Hence from the string, the individual charters can also be directly accessed. Strings are created by using single quotes or double quotes or triple quotes.
Example: >>>s1="Python Programming" >>>s2='Python Programs' >>>s3="""Python is a powerful Programming language"""
This will create a string object s1, s2 and s3 using different forms.
Individual characters can be accessed using different subscripts( +ve or –ve) with the string object.
Example >>>s="Python" >>>s[0] ‘P’ >>>s[2] ‘t’ >>>s[5] ‘n’ >>>s[-1] ‘n’ >>>s[-3] ‘h’ >>>s[-6] ‘P’
We can also slice a string. Segment of a string is called a slice.
>>>s[1:3] ‘yt’
If the first index is not mentioned, the slicing will start from 0. >>>s[:3] ‘Pyt’
>>>s[2:6] ‘thon’
If the last index is not mentioned, the slicing will go till the end o…

Files in Python , Exception handling

While a program is running, its data is in main memory. When the program ends, or the computer shuts down, data in memory disappears. To store data permanently, you have to put it in a file. Files are usually stored on a secondary storage device(hard disk, pen drive, DVD,CD etc). When there are a large number of files, they are often organized into directories (also called “folders”). Each file is identified by a unique name, or a combination of a file name and a directory name. By reading and writing files, programs can exchange information with each other and generate printable formats like PDF. Working with files is a lot like working with books. To use a book, you have to open it. When you’re done, you have to close it. While the book is open, you can either write in it or read from it. In either case, you know where you are in the book. Most of the time, you read the whole book in its natural order, but you can also skip around. All of this applies to files as well. To open a fil…

Classes and Objects in Python

Python is an object-oriented programming language, which means that it provides features that support object-oriented programming. The basic components of object oriented programming are classes and objects. A Class is a blue print to create an object. It provides the definition of basic attributes and functions of objects. Object is a running instance of the class having the identity(name), properties( values) and behaviors(functions). The Object oriented program thus consist of object definitions (classes) and most of the computations and functions are mentioned as operations on the object. Each object definition corresponds to some object or concept in the real world, and the functions that operate on these object correspond to the ways real-world objects interact. We have learned objects of string, list, tuple etc…and used the properties and functionalities of these objects which are built into the Python. Now we are going to create our own(user defined) objects. The class provides basic…