Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Programming in Python(8)Notes

The document provides an overview of programming in Python, explaining what a program is and introducing Python as a high-level, interpreted language. It covers Python's features, basic syntax, operators, data types, comments, and flow control statements, including examples of how to write and execute Python code. Additionally, it discusses loops and their differences from conditional statements, providing practical examples for better understanding.

Uploaded by

kumarisanchita75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming in Python(8)Notes

The document provides an overview of programming in Python, explaining what a program is and introducing Python as a high-level, interpreted language. It covers Python's features, basic syntax, operators, data types, comments, and flow control statements, including examples of how to write and execute Python code. Additionally, it discusses loops and their differences from conditional statements, providing practical examples for better understanding.

Uploaded by

kumarisanchita75
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Programming in Python

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

Features of Python Language

 Python is an open source:


 Python is a Beginner's Language:
 Python is Interpreted:
 Python is Interactive:
 Python is Portable
 Python is Object-Oriented

What Python can do ?

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

1. Find the result of (75+85+65)/3 i.e. the average of three marks

2. Find the result of 22/7 * 5 * 5 i.e. the area of circle having radius as 5

3. Find the result of "RAVI"+"Kant"

4. Find the result of "###" * 3

Exiting Python
To exit from Python, you can type any of these commands: • quit() • exit() • Ctrl+D

Writing Python Programs

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.

Step 1: To open the file editor in IDLE, select File


New File.

print("First Number = ", 10)

print("Second Number = ", 5)

print("Addition of 10 and 5 =", 10 + 5)


Program to calculate the square and cube of number 2.

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

Variables and Data Types


Variable Name (Identifiers)
Program to add two numbers

num1=20 num2=15

num3=num1+num2

print("First Number =",num1)

print("Second Number =",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

Types of Comments in Python

There are two types of comments in Python.

• Single line comment

• Multiple line comment

Single line comment In python

we use #(hash sign) which is a special character to start the comment

eg: # This is just a comment.


Multi-line comment

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.

num1=input("Enter the First Number ")


num2=input("Enter the Second Number ")
numAdd=int(num1)+int(num2)
print("The Addition of ", num1, "and", num2, "=",numAdd)

Try writing your code

 To calculate Area of a triangle


 To calculating average marks of 3 subjects
 To calculate Surface Area and Volume of a Cuboid
 Program to calculate the compound interest.
 Program to calculate the simple interest

Difference Between the == and = Operators


The == operator is a relational operator that asks whether the two values are similar to
each other i.e. check similarity of values on left and right side of == sign.
The = operator (assignment) puts the value written on the right into the variable on
the left.

Flow Control Statements


The term flow control refers to the direction any program takes (which way program
control ‘flows’). It is the basic decision-making process in computing which determines
how a computer will respond when it is given certain conditions and parameters. Flow
control statements evaluate multiple expressions which produce TRUE or FALSE as
outcome. You need to determine which action to take and which statements to execute
if outcome is TRUE or what to do when it is FALSE.

if Statement

name=input('Enter your Name')


if name=="SAM":
print ('Hi', name)

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

An else statement doesn't have a condition in code, an else statement


always consists of the following:
The else keyword
A colon
Starting on the next line, an indented block of code
Example 1 to check the password
#
Get a password from the user.
password = input('Enter the password: ')
if password == 'hello':
print ('Password Accepted')
else:
print ('Sorry, This is the wrong password.')
Example 2 To check whether number entered is 5 or greater than 5 or less than
5
x=input('Enter Number ')
num=int(x)

ifnum==5:
print ('Number is exactly five')
elif num>5:
print (' Number is greater than five')
else:
print('Number is less than five')

Example 3 Program to Check if a Number is Positive, Negative or Zero(use


elif)

num = float(input("Enter a number:"))


if num>0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

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

1.#Creating a variable to start the count


count = 0
#Creating while loop
while count < 6:
print (count)
count =count+ 1

2.# Program to add natural


# numbers upto n where
# n is provided by the user
n = int(input("Enter n: "))
# initialize sum and counter
sum = 0
i = 1
while i <= n:
sum = sum + i
i = i+1 # update counter
# print the sum
print("The sum is",sum)

Difference between If statement and 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

Enter your name and display it 5 times.

name=input('Enter Your Name: ')


for I in range(5) :
print ('Hello', name)
Program to display natural numbers from 1 to 50.

for I in range(1, 51):

print (I)

Count backwards.

for I in range(10,0,-1):

print (I)

Program to check if a number is Odd or Even.

num = int(input("Enter a number: "))

if (num % 2) == 0:

print(num , " is an Even Number")

else:

print(num , " is an Odd Number")

Program to display the multiplication table.

c=0

num = int(input("Display multiplication table of? "))

# use for loop to iterate 10 times

for i in range(1,11):

c +=1

print(num,'x',c,'=', num*c)

You might also like