Programming in Python(8)Notes
Programming in Python(8)Notes
What is a program?
A computer program is a collection of instructions that perform a specific task when executed by a
computer. It is usually written by a computer program in a programming language.
What is Python?
Python is a high-level, interpreted, interactive and object-oriented scripting language which is designed
to be highly readable
When we install Python, an IDE named IDLE is also installed. We can use it to run Python on our
computer. IDLE (GUI integrated) is the standard, most popular Python development environment. IDLE is
an acronym of Integrated Development Environment. It lets one edit, run, browse and debug Python
Programs from a single interface. This environment makes it easy to write programs. Python shell can be
used in two ways, viz., interactive mode and script mode. Where Interactive Mode, as the name
suggests, allows us to interact with OS; script mode lets us create and edit Python source file.
print() Function
Try Yourself
2. Find the result of 22/7 * 5 * 5 i.e. the area of circle having radius as 5
Exiting Python
To exit from Python, you can type any of these commands: • quit() • exit() • Ctrl+D
To write entire Python program, you'll type the instructions into the file editor.
The file editor is similar to text editors such as Notepad, but it has some specific features for typing
source code.
print('Square of 2 is ',2*2)
print('Cube of 2 is ',2*2*2)
Python Operators
Operators are special symbols which represent computation. They are applied on operand(s), which can
be values or variables. Same operators can behave differently on different data types. Operators when
applied on operands form an expression. Operators are categorized as Arithmetic, Relational, Logical and
Assignment. Value and variables when used with operator are known as operands.
Arithmetic Operators
Comparison Operator
num1=20 num2=15
num3=num1+num2
print("Addition =",num3)
Datatypes
Every value in Python has a datatype. Since everything is an object in Python programming, data types
are actually classes and variables are instance (object) of these classes. There are various data types in
Python. Some of the important types are mentioned below in the image
Python Comments
A comment is text that doesn’t affect the output of a code, it is just a piece of text to let someone know
what you have done in a program or what is being done in a block of code
To have a multi-line comment in Python, we use triple single quotes(‘ ’ ’) at the beginning and at the end
of the comment
Eg ''' This is a
multi-line
comment '''
input() Function
It is a string message which prompts for the user input. If the input function
is called, the program flow will be stopped until the user has entered
input and then ended the input with the return key.
if Statement
Flow control statements often start with a part called the condition and all
are followed by a block of code called the clause.
All flow control statements end with a colon and are followed by a new
block of code. The ‘if’ statement's clause is the block such as print('Hi, SAM.').
In Python, the body of the if statement is indicated by the indentation.
Body starts with an indentation and the first unindented line marks the end.
If….else Statements
ifnum==5:
print ('Number is exactly five')
elif num>5:
print (' Number is greater than five')
else:
print('Number is less than five')
Looping in Python
Loop can be defined as one of the basic logical structures of computer
programming. Defining loops allows computers to perform particular tasks
repeatedly. The need for defining the loop in a computer program arises for
various reasons depending on the tasks to be performed. Computer
programming languages require loops so that the codes execute the actions as
many times as needed. Let's consider a situation when you want to write Hello,
World! five times. Here is a simple program to do the same:
While Loop
While loops are known as indefinite or conditional loops. They will keep
iterating until certain condition is met. The code in a while clause will be
executed as long as the while statement's condition is True.
What is iteration?
In terms of programming, it is a process wherein a set of instructions or
structures are repeated in a sequence a specified number of times or until the
condition is met.
Examples While Loop
An if statement checks whether an expression is true or false and then runs the code
inside the statement only if it is true. The code inside the If statement is only run once.
A while statement is a loop, so it continues to execute the code as long as long the
expression is true.
for Loop
The while loop keeps iterating while its condition is True (which is the reason for its
name), but what if you want to execute a block of code only a certain number of
times? You can do this with a for loop statement and the range() function.The for
loop is a Python statement i.e. an iteration statement which allows a code block to be
repeated a certain number of times.
Example
print (I)
Count backwards.
for I in range(10,0,-1):
print (I)
if (num % 2) == 0:
else:
c=0
for i in range(1,11):
c +=1
print(num,'x',c,'=', num*c)