Good To Know:


Oopsie!Either you knew everything or there was a problem in fetching my facts

Posted by : Unknown Saturday, 24 August 2013

python logo


Hey guyz in the previous class you have seen how to evaluate various expressions using python as a calculator.in this class lets see how you can remember those results and use them later in your programs.

                                                   Lets start by calculating area of a rectangle.as a quick reference,the area of a rectangle is obtained by the product of its length and its width

rectangle area

The area can easily be calculated as below
product in python

But in most cases you don't need python to do such simple stuff.i mean even a simple calculator can be used to do such stuff! ok now lets see how to calculate the areas of multiple triangles by introducing the terms length,width into the pythons memory.

                                        We do it by introducing variables.a variable is something which can hold some value in it,in the python as memory class we've talked about these variables.lets see how to use variables in python.In python you need not specify the variable type like in other programming languages,python shell is smart enough to understand what kind of variable it is basing on the value stored in it.Giving a value to a variable is called an assignment operation.it can be done very simply by using the ' = ' sign

Variable_name = value or Expression

As you can see above,a variable can take either a value or an expression in case you assigned an expression to a variable,first the expression is evaluated and the resulting output is stored into the variable.

                                                          Lets try the above instance of calculating the area of a rectangle,this time using variables.

area using pythpn variables

In the above example,
length =10 assigns the value 10 to length variable,width = 50 stores 50 in variable width
The variable area is assigned to the expression length*width so,first the expression length*width is evaluated,then the result is stored in variable area.
just typing in the name of the variable and executing it displays the value stored in the variable like in the last line.

           Now lets try changing the values of one of them and see the resulting area 

variable area using python

               wait,what?wasn't i supposed to get 100 as area? its a no because,the variable area contains the result of length*width but not the expression itself,so in first case,the expression evaluates to 50 and area points to 50.So even when we change one of the variable,area still points to 50,now to remove that old memory link and to create a new one,you need to run the assignment statement again,

updated area in python
   Yup there it goes!the updated value of area.

Rules for naming a variable:
  • You can give any name to a variable but it is advised to use related names which say what data it holds rather than generalized variable names like x,y,a,b etc
  • Variables in python are case sensitive,which means " num1 " is not the same as "Num1"
  • A variable name must start with either a "_ "(underscore) or any alphabet
  • A variable name should not start with a number
  • a variable name can contain alphabets,numbers and a underscore no other special symbols are allowed
  • _area,Area2,twice_of_area etc all are allowed variable names

Summary:


Assignment statements

The general form of an assignment statement:
variable = value or expression

Variable names

The rules for legal Python names:
  1. Names must start with a letter or _.
  2. Names must contain only letters, digits, and _.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Translate

Blogger Tips and Tricks

Popular Post

Random post

- Copyright © smartify me -

- Administered by Krishna Chaitanya -
Google+