Good To Know:


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

Showing posts with label Learn Python. Show all posts

Learn Python --Class 5 Defining Functions

logo python
User defined functions!they are pretty much very important in any programming language,and basically programming is all about it.in this class lets see hoe to define a function in python

Lets try defining our  own function for addition of two numbers,

python add function


If you can just look into the image,basically a function in python has Function name,body of the function,and a return statement.

  • A function must start with the key word "def" which says python that we are about to define a function.
  • Then the name of the function followed by a set of parentheses inside which any number of parameters separated by a comma ( , )can be passed to the function.
  • and after the parentheses a colon,which indicate we are about to write what happens when the particular function is called.so,the head of our function would look like,
def name_of_function(parameter1,parameter2......) :



  • parameters of a function are simply those initial data items with which you want to call a function,for example,it we want our function to give us the product of two numbers,you should say it what are those numbers for which you need a product,unless you say it,it doesn't know them (see computer ain't dat smart ;) )
  • Now comes the body of the function it is where you write what happens when the function is called .in the above example,we asked python to add up the values of a and b,then store it in variable c,this small piece of code is the body of the function add()
  • And all the functions end with a return statement.it is used to return a memory location reference when called.a return statement indicates the end of the function.unlike other languages you need no deal with messed up parenthesis and brackets,def starts a function,return statement ends it,thats all!
  • Now after typing the return statement you might wanna hit the enter key two times in order to get the prompt again.now lets test our function,
  • To use a function you simply need to call it with its name and the required number of arguments or else you will end up with a type error
python type error
  • When there are no errors you would get the output


python calling function

Arguments Vs Parameters :

   Its always been a confusing scenario whether to use the word arguments or parameters,well atleast for me,so i figured out i would just tell you the basic difference between an argument and a parameter,
difference between arguments and parameters

  • If you can see in the above image in case 1,a and b are called the parameters of the function,and in case 2,the values 5,7 are called the arguments of the function.you can simply say that those things in the parenthesis while defining a function are called as parameters and things in brackets while calling up the function are called arguments.
  • And by the way, # is used to mark a single line comment ie., any thing in a line starting from # is ignored by the compiler,comments are used to make programs more readable,off course we can ignore them in such small codes,but when the code gets bigger,the are definitely of a great help
Lets just have a quick look on how to save and load code in shells,

  • The python code files are called as shells.to save a shell,first complete typing you code,then goto file -> click save -> give it a name and click save.python shells will be with an extension of " *.py "
saving python shell


save as .py

  • And to load a shell,you can go to the location of the shell,right click and select edit with idle,
edit shell in idle

  • Then in the shell window,goto run and click run module
run module-python



  • Once you load a shell into python window,you can now access all the functions defined in that shell.

Summary:



The general form of a function definition:


def function_name(parameters):
                       body
                       return statement

  • def: a keyword indicating a function definition
  • function_name: the function name
  • parameters:
    • the parameter(s) of the function, 0 or more and are separated by a comma
    • a parameter is a variable whose value will be supplied when the function is called
  • body:
    • 1 or more statements, often ending with a return statement



The general form of a function call:

function_name(arguments)


Saturday, 31 August 2013
Posted by Unknown

Learn Python -Class 4: variables

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 _.

Saturday, 24 August 2013
Posted by Unknown

Learn Python-Class 3: Python And Computer Memory

python logo

In this Class lets see what actually is computer memory and how python deals with it.And don't worry in case you feel a bit uncomfortable with this topic,you can safely skip it,but just go through the summary


                                                There are actually two parts of a program one that is visible to you,like playing a game,watching a movie etc and the most important is the other part which is not visible to us.Each time you hit a key there are a ton of processes and computations taking place inside your computer.out of those,memory management is a high order one.

                                     You can think of Computer memory as boxes,lots and lots of boxes which can store things in the form of binary data.and each box has a name which is called as "the Memory Address".Each box is capable of containing either a one or a zero nothing else ( Every thing is converted into a series of ones and zeros ).these tiny units of memory are called bits.and 8 bits make up a byte and 1024 bytes make a Kilo Byte



1 Bit = Binary Digit
8 Bits = 1 Byte
1024 Bytes = 1 Kilobyte 
1024 Kilobytes = 1 Megabyte 
1024 Megabytes = 1 Gigabyte 
1024 Gigabytes = 1 Terabyte 
1024 Terabytes = 1 Petabyte 
1024 Petabytes = 1 Exabyte
1024 Exabytes = 1 Zettabyte 
1024 Zettabytes = 1 Yottabyte 
1024 Yottabytes = 1 Brontobyte
1024 Brontobytes = 1 Geopbyte

And each  data item be it an integer a float or any other occupies a number of bytes.

int – occupies 2 bytes(16-bit compiler) or 4 bytes (32-bit compiler) of memory
 float - occupies 4 bytes of memory
 double – occupies 8 bytes of memory
 char – occupies 1 byte of memory


Memory Address:
 Memory address is generally represented with a pre fix of x like x102 represents the memory block with address 102 similarly x563,x784 etc

memory addresses




we generally use variables to keep track of the memory addresses.A variable can be considered as a named location or memory cell which can contain different data at different instances,hence it is called a variable.

 Python keeps variables in a separate list from values. A variable will contain a memory address, and that memory address contains the value. The variable then refers to the value.Python picks up the available memory addresses for you.so you need not worry about allocating memory addresses. Infact you are no allowed to choose into what memory address your data goes

Terminology

A value has a memory address.
A variable contains a memory address.
A variable refers to a value.
A variable points to a value.

Summary:

Computer memory is a long list of storage locations each with a specific address.

Variables are a way to keep track of values stored in computer memory. A variable is a named location in computer memory. Python keeps variables in a separate list from values. A variable will contain a memory address, and that memory address contains the value. The variable then refers to the value. Python will pick the memory addresses for you.

variable

Tuesday, 20 August 2013
Posted by Unknown

Translate

Blogger Tips and Tricks

Popular Post

Random post

- Copyright © smartify me -

- Administered by Krishna Chaitanya -
Google+