0% found this document useful (0 votes)
74 views33 pages

Introduction To Python: Python Is A High Level, General Purpose Programming Language

Uploaded by

shobhasharanya65
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)
74 views33 pages

Introduction To Python: Python Is A High Level, General Purpose Programming Language

Uploaded by

shobhasharanya65
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

Introduction to python

A program is a set of instructions that govern the processing. We can give


the program some input, the program carries out some instructions on the
given input and produces an output.

Python is a popular programming language. It was created by Guido van


Rossum, and released in 1991.

Python is a high level, general purpose programming language.

It is used for: web development (server-side), software development,


mathematical analysis, system scripting, etc.
Working in Python

Once we have installed Python in our computer, we are ready to work on it.

We can work in python in different ways:

1) In Interactive mode - when you execute one command at a time


2) In Script mode - when we save all commands in the form of a program
file.
Variables and Expressions

Variables are named places to hold some values in a program. These


values can be changed when needed.
Example:
age = 10
print(age)

Arithmetic operators are symbols to depict some arithmetic


calculations, such as + (addition), - (subtraction), * (multiplication), /
(division)
Variables and Expressions
Floor Division operators //

This operator performs division in which the whole part of the result is
given as output and the fractional part is truncated

Example: a = 15
b=3
c = a//b
print(c)
Variables and Expressions
Modulus Operator %

This operator finds the modulus, i.e. the remainder of the division
between two operands.

Example:
c = 24 % 7
print (c)
Variables and Expressions
Expressions refers to a combination of variables/values and operators
that generates another value.

Example
A = 10
B = 20
C = 30
Sum = A + B + C #expression
Data Types
Data can be of different types e.g., character, integer, real, string, etc.

String: Anything enclosed in quotes represent string data in Python


Example: str = “Hello world”

Integer: Numbers without fractions represent integer data


Example: sum = 100

Real: Numbers with fractions represent real data.


Example: percent = 72.61
The input() function
To get input from user iteratively, we can use built-in function input().

Example: name = input(“What is your name?”)


age = input(“Enter your age:”)

The print() function


To display output to the user on console, we can use built-in function
print().

Example: name = “Harry”


print(“My name is “, name)
Python Casting
At times we would want to specify a type on to a variable. This can be
done with casting.

Example:
y = int(2.8) # y will be 2
z = int("3") # z will be 3

a = int(input('Enter the any integer:'))


b = float(input('Enter the marks of English:'))
Jupyter Notebook
Jupyter Notebook is an interactive open source web application for developing
data science python projects.
Web application is an application which we can access using a web browser.
Jupyter Notebook
Jupyter Notebook
1. Write a program to accepts two integers and print their sum.

a=int(input('Enter the first integer:'))


b=int(input('Enter the second integer:'))
Sum=a+b
print('The two integers are:', a, b)
print('The sum of two integers are:', Sum)
2. Write a program that accepts radius of a circle and prints its area.

r=int(input('Enter the radius of circle:'))


Area=3.14*r*r
print('The area of the circle is:', Area)
3. Write a program that inputs a student’s marks in three subjects
(out of 100) and prints the percentage marks.
print('Enter the marks of three subject out of 100')
a=float(input('Enter the marks of first subject:'))
b=float(input('Enter the marks of second subject:'))
c=float(input('Enter the marks of third subject:'))
P=(a+b+c)/3
print('The percentage marks are:', P,'%')
4. Write a program to read two numbers and prints their quotient
and remainder.
a=float(input('Enter the dividend:'))
b=float(input('Enter the divisor:'))
Q=a//b
R=a%b
print('The quotient is:', Q)
print('The remainder is:', R)
5. Write a program that reads the number n and print the value of n2,
n3 and n4.
a=float(input('Enter the value of n:'))
b=a**2
c=a**3
d=a**4
print('The value of n^2 is:', b)
print('The value of n^3 is:', c)
print('The value of n^4 is:', d)
Conditional statement
Python supports the usual logical conditions from mathematics.
● Equals: a == b
● Not Equals: a != b
● Less than: a < b
● Less than or equal to: a <= b
● Greater than: a > b
● Greater than or equal to: a >= b

A conditional statement is written by using the if keyword.


Conditional statement
A conditional statement is written by using the if keyword.

The elif keyword is Python's way of saying "if the previous conditions were
not true, then try this condition”

The else keyword catches anything which isn't caught by the preceding
conditions.

Python relies on indentation (whitespace at the beginning of a line) to


define scope in the code.
Conditional statement
if condition 1:

Statement 1

elif condition 2:

Statement 2

else:

Statement 3
6. Write a program that accepts the age and print if one is eligible to
vote or not.
age=int(input('Enter your age:'))
if age>=18:
print('You are eligible to vote')
else:
print('You are not eligible to vote')
7. Write a program to input percentage marks of a student and find the grade
as per following criterion:
Marks Grade: >=90 - A; 75-90 - B; 60-75 - C; Below 60 - D
a=float(input('Enter the percentage marks:'))
if a>=90:
print('The student has got an A grade')
elif a>=75 and a<90:
print('The student has got a B grade')
elif a>=60 and a<75:
print('The student has got a C grade')
else:
print('The student has got a D grade')
8. Write a program to find the largest integer among three integers.

a=int(input('Enter the first integer:'))


b=int(input('Enter the second integer:'))
c=int(input('Enter the third integer:'))
if a>b and a>c:
print(a, 'is the largest integer')
if b>a and b>c:
print(b, 'is the largest integer')
if c>a and c>b:
print(c, 'is the largest integer')
Nested if condition statements
We can have an if…elif…else statement inside another if…elif…else statement.
This is called nesting in computer programming

if condition 1:
if condition 2:
Statement 1
else
Statement 2
else
Statement 3
9. Write a program to check whether an input number is positive,
negative or zero
a=float(input('Enter the number:'))
if a>=0:
if a==0:
print('The number is zero')
else:
print('The number is a positive number')
else:
print('The number is a negative number')
Data structures in Python
• A data structure is a way of organizing data in computer memory for efficient
access in the program.
• Built-in data structures in Python can be divided into two broad categories:
mutable and immutable.

• Mutable data structures are those which we can modify -- for example, by
adding,removing, or changing their elements. Python has three mutable data
structures: lists, dictionaries, and sets.
• Immutable data structures, on the other hand, are those that we cannot modify
after their creation. The only basic built-in immutable data structure in Python is
a tuple.
List in Python
• Lists are used to store multiple items in a single variable.
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second item has index
[1] etc.
• To determine how many items a list has, use the len() function

Examples:
thislist = ["apple", "banana", "cherry"]
print(thislist)
print(len(thislist))
10. Create a list of students selected in Science Quiz with following name -
Arjun, Sonakshi, Vikram, Sonal, Isha and Karthik. Perform following tasks in the
list:
1. Print the list
2. Delete name “Vikram” from the list
3. Add name “Jay” at the end
4. Remove the item at the second position in the list
student_list = [ “Arjun”, “Sonakshi”, “Vikram”, “Sonal”, “Isha”, “Karthik”]
print(“Students selected for science quiz are: “, student_list)
student_list.remove(“Vikram”)
student_list.append(“Jay”)
student_list.pop(1)
print(“Students list after changes: “, student_list)
11. Create a list of numbers - [23, 12, 5, 9, 65, 44]
1. Print the length of the list
2. Print the elements from second to fourth position using positive
indexing
3. Print element from position third to fifth using negative indexing
num_list = [23, 12, 5, 9, 65, 44]
list_len = ln(num_list)
print(“Length of the list is”, list_len)
print(“elements from 2nd to 4th position using positive indexing”, num_list[1:3])
print(“elements from 3rd to 5th position using negative indexing”, num_list[-1:-3])
Loops
A loop is a control structure that allows a set of statements that need to be repeated to
execute until a specified condition or exit condition is met.
In Python we use For loop and While loop to handle looping

while loop is used to execute a block of statements repeatedly until a given condition is
satisfied.
Example:
while expression:
statement(s)
for loop is used for sequential traversal
Example:
for iterator_var in sequence:
statement(s)
12. Write a program to calculate the factorial of a number.

num=int(input('Enter a number:'))
fact=1
a=1
while a<=num:
fact*=a
a+=1
print('The factorial of',num,'is',fact)
13. Write a program to print 10 natural numbers
print(“First 10 natural numbers are:”)
for i in range(1, 11):
print(i)
14. Create a list of first 10 even numbers, add one to each list item
and print the final list
even_num = [2, 4, 6, 8, 10, 12 ,14, 16, 18, 20]
for i in range(0, len(even_num)):
even_num[i] = even_num[i] + 1
print(“Updated list is:” even_num)

You might also like