PYTHON:-
ython is a powerfull programming langudge. It helps the programmer find slution to a given problem without warrying about the technical details of the programming langudge viz. structor and syntax.
It supports high level programming langudge with both procedure and objet-orit enter programming langudge propertis and is sutable for scripting and development of various appliction.
The pthon programming langudge is introduced by
MODULE:-consider a module to be the same as a code library.
A file containing a set of of fuction you wont to include in your appliction.
CREATE A MODULE:- To creat a module just save the code you want in a
file with the extension.
.py
Ex:- save thsi code in a file named
mymodule.py
def greeting (name)
print(“Hello”+name)
USE A MODULE:- Now we can use the module we just created ,
by using the import statement:
Ex:-
import the module named mymodule, and call the greeting function.
import mymodule
mymodule.greeting(“tec santosh”)
NOTE:-
When using a fuction from amodule, use the syntex:
module_name,function_name
VARIABLES IN MODULE:-
The module can cantain fuctions, as alredy
decribed, but also variables of all type (arrays,
dictinaries, objects etc):
Ex:-
save this code in the file
mymodule.py
(1) import mymodule
persion 1={
“name”:”tec santosh”,
“age”:”36″,
“country”:”India”
}
a=mymodule.persion 1[“age”]
NAMING A MOPDULE:-
You can name the module file whtever you like ,
but it module have the file extension .py
RE-NAMING A MODULE:-
You can c reat an alias whe you import a module,
by using the as ke word.
Ex:-
create an alias for my module called mx:
import mymodule as mx
a= mx.persion 1[“age”]
print(a)
BUILT-IN MODULE:-
There are several built-in module in python , which you
can import whenever you like
Ex:-
Import and use the platform module
import platform
x=platform.system()
print(x)
Using the dir() Function:-
There is a built-in function to list all the function names
(or voriable names) in a module. The dir() functsion.
Ex:-
list all the defined names beloing to the platform module.
import platform
x=dir(platform)
print(x)
Note:-
The dir() fuction can be used on all module also the
ones you create yourself
IMPORT Form MODULE:-
Import only parts form a module, by using the from
keyword.
Ex:-
The module named mymodule has onefuction and one dictionary.
def greeting(name):
print(“Hello,”+name)
persion 1={
“name”:”tec santosh”,
“age”:”36″,
“country”:”India”
}
a=mymodule.persion 1[“age”]
1. Write a program to accept a number from user and print its multiplication table in the
format (2*1=2 upto 2*10=20)
Ans. for i in range(21):
if(i>0):
print(n,’*’,i,’=’,n*i)
2.Write a program to display -10 to -1 using for loop
Ans. for i in reversed(range(11)):
print(‘-‘,i)
3.Write a program to input a number and count the total number of digits in a number.
Ans. n=int(input(“Enter number:”))
count=0
while(n>0):
count=count+1
n=n//10
print(“The number of digits in the number are:”,count)
4. Write a program which will find all such numbers which are divisible by 4 but are not
the multiple of 6 between 1 and 100.
Ans. a= 1
n= 100
for i in range(a, n+1):
if( i%4==0 and i%6!=0 ):
print (i)
5. Write two different programs to print a series of numbers by using
1. Break statement
2. Continue statement
Ans . Break Statements
Break statements exist in Python to exit or “break” a for or while conditional loop. When the loop ends, the code picks up from and executes the next line immediately following the loop that was broken.
Example of Break Statements
numbers = (1, 2, 3)
num_sum = 0
count = 0for x in numbers:
num_sum = num_sum + x
count = count + 1
print(count)
if count == 2:
break
. Continue statement
The continue statement is used to skip code within a loop for certain iterations of the loop. After the code is skipped, the loop continues where it left off.
Example of continue statement
for x in range(4):
if (x==2):
continue
print(x)
