PYTHON FOR ABSOLUTE BEGINNERS

PYTHON FOR ABSOLUTE BEGINNERS

PYTHON

Python is a widely used general purpose, high level programming language as in THE THOBIE INDEX it is rated the second best programming language. Its design emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of codes than would be possible in languages such as c++  or java. The language provides construct intended to enable clear programs on both a small and large scale.

Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.[26]

 

Python interpreters are available for installation on many operating systems, allowing Python code execution on a wide variety of systems. Using third-party tools, such as Py2exe or Pyinstaller,[27] Python code can be packaged into stand-alone executable programs for some of the most popular operating systems, allowing the distribution of Python-based software for use on those environments without requiring the installation of a Python interpreter.

Know More About Python :- Introduction To Python Programming

Installation and documentation

If you use Mac OS X or Linux, then Python should already be installed on your computer by

default. If not, you can download the latest version by visiting the Python home page, at

http://www.python.org

where you will also find loads of documentation and other useful information. Windows users can

also download Python at this website. Don’t forget this website; it is your first point of reference

for all things Python. You will find there, for example, reference [1], the excellent Python Tutorial

by Guido van Rossum. You may find it useful to read along in the Tutorial as a supplement to

this document.Getting started

For More Help Related Installing Python Read This :- Python Installation And Path Settings

1.Getting started

1.1 Running Python as a calculator

1.2 Quitting the interpreter

1.3 Loading commands from the library

1.4 Defining functions

1.5 Files

1.6 Testing code

1.7 ScriptsPython commands

2.Python commands

2.1 Comments

2.2 Numbers and other data types

2.2.1 The type function

2.2.2 Strings 2.2.3 Lists and tuples

2.2.4 The range function

2.2.5 Boolean values

2.3 Expressions

2.4 Operators

2.5 Variables and assignment

2.6 Decisions

2.7 Loops

2.7.1 for loop

2.7.2 while loop

2.7.3 else in loops

2.7.4 break, continue, and pass

2.8 Lists

2.8.1 Length of a list; empty list

2.8.2 Sub lists (slicing)

2.8.3 Joining two lists

2.8.4 List methods

2.9 Strings

 2.1 Running Python as a calculator

The easiest way to get started is to run Python as an interpreter, which behaves likes a basic calculator. In the interpreter, you type a command, and Python produces

the answer. Then you type another command, which again produces an answer, and so on.

In OS X or Linux, to start the Python interpreter is as simple as typing the command python

on the command line in a terminal shell. In Windows, assuming that Python has already been installed, you need to find Python in the appropriate menu. Windows users may choose to run

Python in a command shell (i.e., a DOS window) where it will behave very similarly to Linux or

OS X.

For all three operating systems (Linux, OS X, Windows) there is also an integrated development

environment for Python named IDLE. If interested, you may download and install this on your com-

puter.2  For help on getting started with IDLE see http://hkn.eecs.berkeley.edu/~dyoo/python/idle_int

Once Python starts running in interpreter mode, using IDLE or a command shell, it produces a

prompt, which waits for your input. For example, this is what I get when I start Python in a

command shell on my windows box:

python

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32

Type “copyright”, “credits” or “license()” for more information.

>>>

where the three symbols >>> indicates the prompt awaiting my input.

So experiment, using the Python interpreter as a calculator. Be assured that you cannot harm

anything, so play with Python as much as you like. For example:

>>> 2*1024

2048

>>> 3+4+9

16

 

In the above, we first asked for the product of 2 and 1024, then we asked for the sum of 3, 4, and 9

. Note that multiplication in Python is represented by *,

addition by +, and exponents by **; you will need to remember this syntax.

>>> 2**1000

Do not worry about the code below we will discuss this later in loops.

Here is another example, where we print a table of perfect squares:

>>> for n in [1 ,2 ,3 ,4 ,5 ,6]:

print n **2

1

4

9

16

25

36

>>>

2.2 Quitting the interpreter

In a terminal you can quit a Python session by CTRL-D. (Hold down the CTRL key while pressing

the D key.) In IDLE you can also quit from the menu.

If the interpreter gets stuck in an infinite loop, you can quit the current execution by CTRL-C.

2.3 Loading commands from the library

Python has a very extensive library of commands, documented in the Python Library Reference

Manual [2]. These commands are organized into modules. One of the available modules is especially

useful for us: the math module. Let’s see how it may be used.

Go on try this code

>>>import math

>>>math.sqrt(81)

9.0

Sqrt stands for square root

>>>math.pow(2, 3)

8

Pow stands power 2 to the power of 3 = 8

 

>>>

   2.4 Defining functions

It is possible, and very useful, to define our own functions in Python. Generally speaking, if you

need to do a calculation only once, then use the interpreter. But when you or others have need to

perform a certain type of calculation many times, then define a function. For a simple example,

the compound command

>>> def  f(x):

… return x*x

defines the squaring function f(x) = x2, a popular example used in elementary math courses. In

the definition, the first line is the function header where the name, f, of the function is specified.

Subsequent lines give the body of the function, where the output value is calculated. Note that

the final step is to return the answer; without it we would never see any results. Continuing the

example, we can use the function to calculate the square of any given input:

>>> f(2)

4

>>> f (2.5)

6.25

The name of a function is purely arbitrary. We could have defined the same function as above,

but with the name square instead of f; then to use it we use the new function name instead of

the old:

>>> def square (x):

… return x*x

>>> square (3)

9

>>> square (2.5)

6.25

Actually, a function name is not completely arbitrary, since we are not allowed to use a reserved

word as a function name. Python’s reserved words are: and, def, del, for, is, raise, assert,

elif, from, lambda, return, break, else, global, not, try, class, except, if, or, while,

continue, exec, import, pass, yield.

By the way, Python also allows us to define functions using a format similar to the Lambda

Calculus in mathematical logic. For instance, the above function could alternatively be defined in

the following way:

>>> square = lambda x: x*x

Here lambda x: x*x is known as a lambda expression. Lambda expressions are useful when you

need to define a function in just one line; they are also useful in situations where you need a

function but don’t want to name it.

Usually function definitions will be stored in a module (file) for later use. These are indistinguish-

able from Python’s Library modules from the user’s perspective.

2.5 Files

Python allows us to store our code in files (also called modules). This is very useful for more

serious programming, where we do not want to retype a long function definition from the very

beginning just to change one mistake. In doing this, we are essentially defining our own modules,

just like the modules defined already in the Python library. For example, to store our squaring

function example in a file, we can use any text editor3 to type the code into a file, such as

def square (x):

return x*x

Notice that we omit the prompt symbols >>>, … when typing the code into a file, but the

indentation is still important. Let’s save this file under the name “SquaringFunction.py” and then

open a terminal in order to run it:

 

Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32

Type “copyright”, “credits” or “license()” for more information

>>> from SquaringFunction import square

>>> square (1.5)

2.25

Notice that I had to import the function from the file before I could use it. Importing a command

from a file works exactly the same as for library modules. (In fact, some people refer to Python

files as “modules” because of this analogy.) Also notice that the file’s extension (.py) is omitted

in the import command.