Important Announcement
PubHTML5 Scheduled Server Maintenance on (GMT) Sunday, June 26th, 2:00 am - 8:00 am.
PubHTML5 site will be inoperative during the times indicated!

Home Explore Python class iX notes

Python class iX notes

Published by Jahanvi Sharma, 2023-02-22 01:35:30

Description: Python class iX notes

Search

Read the Text Version

Python introduction: Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during 1985- 1990. Like Perl, Python source code is also available for free. Features of Python or advantages of python or benefits of using python: Python is Interpreted − Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. Python is Object-Oriented − Python supports Object-Oriented style or technique of programming that encapsulates code within objects. It supports automatic garbage collection. Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly. Portable − Python can run on a wide variety of hardware platforms and has the same interface on all platforms. GUI Programming − Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix. Modes of working in python: • After downloading and installing python • You will open IDLE(Integrated development and learning environment) . • There are two modes to work in python: – Interactive mode – Script mode • Interactive mode- In this mode line gets executed as soon as you press enter • Script mode- When you want to write full program then execute it then you can use script mode In interactive mode, instructions are given in front of python prompt (>>>)in python shell. Python executes the given instruction and shows the output there itself after pressing enter. In script mode, Python codes are store in a file with .py extension and

are executed together in one go as a unit. The saved instructions are known as python script or python program. Variables: Variables are container that holds values in python code. Variable naming rules: 1. It should start with alphabet or an underscore_ 2. It cannot contain any special symbol other than _ 3. It can have numbers but cannot start with a number 4. Keywords cannot be used as identifier 5. There should be no space between a variable name Print statements: print(“hello”) output: hello print(“hello”, “world”) output: hello world print(“hello”, 123) output: hello 123 print(1*2) output: 1 print(45%5) output: 0 print(10//5) output: 2 print(10/5) output: 2.0 print(10,20,30) output: 10 20 30 DataTypes in Python: Integer: whole numbers Float: decimal numbers String: any thing that is enclosed in “” or ‘’, “45” is also a string Write correct datatype of values. a. 1023 int b. 17.54 float

c. “my fat cat” str d. “23.89” str e. “3456” str f. 987.0 float g. 78654 int If a=20 and b=30 What will be output of Print(a+b) Output>>>50 If a=”20” and b=”30” What will be output of Print(a+b) Output>>>”2030” Reason: a and b both are string Taking input: Input() function is used to take input. For example: a= input() a is variable any value entered by user will be stored in variable a. Input function always gives data in type string. If we want data in any other type then data type conversion is required. For example if we want data in type integer or float we can use function int() or float()to convert string data to integer type or float datatype: a= int(input(“enter value of a”)) b=float(input(“enter value for b”)) Arithmetic operators: to add to numbers +

To subtract two numbers - To multiply two numbers * To divide two numbers / , // / gives answer in float // gives answer in int To give remainder %(modulus) To calculate power ** 2**3 (evaluates 2 to the power 3, answer is 8) Basic program: 1. Write a program to calculate simple interest p=float(input(“enter principle”)) r=float(input(“enter rate”)) t=float(input(“enter time”)) si=p*r*t\\100 print(“simple interest is”, si) 2. Write program to calculate area of circle: R=int(input(“enter value for radius”)) A=22/7*R*R print(“area of circle is”,A) 3. Write a program to take name as input and print good morning with that name. name=input(“enter your name”) print(“good morning”,name) 4. Write a program to a symbol as input and a number n as input. Display that symbol n times as output. S=input(“enter symbol to print”) N=int(input(“enter how many times you want to repeat symbol”)) print(S*N) 5. Write a program to take a number as input and calculate its square root of that number and print it. N=int(input(“enter a number”)) print(“Square root of number is”,N**(1/2))

List: • List is collection of data or collection of an ordered sequence of data. • Data stored in list are called its elements • Elements are enclosed in square brackets[ ] • Elements can be of same type or can be heterogeneous. Declaring a list: • <name>=[element1, element2,element3] • List1=[1,3,4,5,6,7] Example: L1=[1,2,3,4,5] print(L1[2]) output will be >>>3 Function to add element in list append() extend() insert() Add a single element at a Add one element at end of Add multiple element at given location or index the list the end of the list L1=[1,2,3,4,5] L1.append(6) print(L1) l1.extend([7,8,9]) print(L1)

print(insert(0,0)) print(L1) output will be: [1,2,3,4,5,6] [1,2,3,4,5,6,7,8,9] [0,1,2,3,4,5,6,7,8,9] Function to delete data from list. pop() remove() Delete data from given index, if index is Delete given element from list not mentioned delete last index data. L1=[1,2,3,4,5] L1.pop(1) #this line will delete index 1 element that is 2 L1.pop() #This line will delete last element that is 5 inn this case L1.remove(4) #this will delete 4 from list Program: 1. Write a program to take a list of 5 integer: a. Take a number as input and add it at the end of list b. Take a number as input and add it at a given position c. Take three number as input and add them at the end of the list L1=eval(input(“enter a list of 5 numbers)) N1=int(input(“enter a number to add in a list”)) L1.append(N1) N2=int(input(“enter a number to add in a list”)) pos=int(input(“enter a position to add a number ”)) L1.insert( pos,N2) N3=eval(input(“enter three number to add in a list”)) L1.extend(n3)


Like this book? You can publish your book online for free in a few minutes!
Create your own flipbook