Professional Documents
Culture Documents
NO: 4
BASIC PYTHON COMMANDS
DATE:
AIM:
PROCEDURE:
Starting Python
$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
Arithmetic expressions
Indentation
Python programs get structured through indentation i.e code blocks are defined by
indentation,Python provides no braces to indicate block of code for class ,functions and
flow control.
The number of spaces in the indentation is not fixed value but all statements within the
block must be indented the same amount.
Control Flow
if condition:
statement1
statement2...
elif condition:
statement1
statement2...
else:
statement1
statement2..
initialization;
while(condition)
{
//Code block to execute something
}
Lists: There are the most versatile sequence type. The elements of a list can be any object
and lists are mutable.
The wind speed data (in km/hr) from a weather station, for a single day, obtained every 2
hours, starting from 12:00am: 3, 5, 3, 2, 0, 0, 5, 5, 11, 5, 10, 2.
List Methods
mylist=[10,20,30,40,50]
m=len(mylist)
print(mylist)
mylist.insert(0,5)
print(mylist)
mylist.append(60)
print(mylist)
A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The
differences between tuples and lists are, the tuples cannot be changed unlike lists and
tuples use parentheses, whereas lists use square brackets.These are like lists, But Tuples
are immutable.
tuplex = (2, 4, 3, 5, 4, 6, 7, 8, 6, 1)
slice = tuplex[3:5]
print(_slice)
slice = tuplex[:6]
print(slice)
slice = tuplex[5:]
print(_slice)
slice = tuplex[:]
print(_slice)
slice = tuplex[-8:-4]
print(_slice)
tuplex = tuple("HELLO WORLD")
print(tuplex)
slice = tuplex[2:9:2]
print(_slice)
_slice = tuplex[::4]
print(_slice)
slice = tuplex[9:2:-4]
print(_slice)
Dictionary in Python is an unordered collection of data values, used to store data values
like a map, which unlike other Data Types that hold only single value as an element,
Dictionary holds key:value pair. Key value is provided in the dictionary to make it more
optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is
separated by a ‘comma’.
A Dictionary in Python works similar to the Dictionary in a real world. Keys of a Dictionary
must be unique and of immutable data type such as Strings, Integers and tuples, but the
key-values can be repeated and be of any type.
dict={‘Name’:’SteveJobs’,’Age’:70,’’class’:’First’};
print(dict[‘Name’])
print(dict[‘Age’])
Strings and slicing
These are a special type of sequence that can store only characters and having special
notations.
String multiplication
Print a 5 step right-angled triangle using string multiplication.
>>> max = 5
>>> for i in range(0, max):
... print "*" * (i+1)
...
*
**
***
****
*****
>>>
EX.NO: 5
PYTHON PROGRAMMING
DATE:
AIM:To write a program to check whether a number entered by user is prime or not.
Algorithm:
Step 1: Start
Step 2: Declare variables n,i,flag.
Step 3: Initialize variables
flag←1
i←2
Step 4: Read n from user.
Step 5: Repeat the steps until i<(n/2)
5.1 If remainder of n÷i equals 0
flag←0
Go to step 6
5.2 i←i+1
Step 6: If flag=0
Display n is not prime
else
Display n is prime
Step 7: Stop
PROGRAM
# take input from the user
num = int(input("Enter a number: "))
# prime numbers are greater than 1
if num > 1:
# check for factors
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
OUTPUT
user@boss:/$ vi prime.py
user@boss:/$ python prime.py
Enter a number: 7
(7, 'is a prime number')
user@boss:/$ python prime.py
Enter a number: 123
(123, 'is not a prime number')
(3, 'times', 41, 'is', 123)
RESULT:
Thus, we implemented a python program to find the prime number.
AIM :
ALGORITHM :
Step 1: Start
Step 2: Declare variables first_term,second_term and temp.
Step 3: Initialize variables first_term←0 second_term←1
Step 4 : Get the input as nterm
Step 5: Display first_term and second_term
Step 6: Repeat the steps until second_term≤nterm
temp←second_term
second_term←second_term+first term
first_term←temp
Display second_term
Step 7: Stop
PROGRAM :
OUTPUT
RESULT:
C. Write a program to find how many times an element occurred in list and find the sum of
elements in the list.
AIM:
To write a program to find how many times an element occurred in list and find the sum of
elements in the list using predefined function and userdefined function
ALGORITHM:
Step 1:Take in the number of elements for the first list and store it in a variable.
Step 2:Take in the elements of the list one by one.
Step 3: Then take in the number to be searched in the list.
Step 4: Use a for loop to traverse through the elements in the list and increment the count
variable.
Step 5:. Display the value of the count variable which contains the number of times a
particular number occurs in a list
#-*-coding:utf-8-*-
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
k=0
num=int(input("Enter the number to be counted:"))
for j in a:
if(j==num):
k=k+1
print("Number of times",num,"appears is",k)
OUTPUT
RESULT:
Thus, we implemented a python program to find how many times an element occurred in
list and find the sum of elements.
AIM:
ALGORITHM
OUTPUT
True
False
{'r': 3, 'w': 2}
RESULT:
AIM:
ALGORITHM:
PROGRAM:
print """
1:Add a contact
2:Search a contact
3:Delete a contact
4:Update a contact
5:View directory
6:Exit"""
choice=input("Enter your choice")
def add(dict1):
name3=raw_input("Enter the new name you want to add: ")
num3=input("Enter the number: ")
dict1[name3]=num3
print dict1
def search(dict1,n,list1,temp):
name2=raw_input("Enter the name whose number is to be found: ")
for i in range(0,n):
if list1[i]==name2:
temp=i
if temp!=100:
print "Number is : ",list2[temp]
def delete(dict1):
name4=raw_input("Enter the name you want to delete: ")
del dict1[name4]
print dict1
def update(dict1,n,list1):
name5=raw_input("Enter the name which you want to update: ")
for i in range(0,n):
if list1[i]==name5:
temp=i
if temp!=100:
num5=input("Enter the new number")
dict1[name5]=num5
print dict1
def view(dict1):
print dict1
if (choice==1):
add(dict1)
elif (choice==2):
search(dict1,n,list1,temp)
elif (choice==3):
delete(dict1)
elif (choice==4):
update(dict1,n,list1)
else:
view(dict1)
OUTPUT
Enter the number of contacts : 4
Enter your name: stevejobs
Enter your phone number: 99123456
Enter your name: hilary
Enter your phone number: 7689092341
Enter your name: kavin
Enter your phone number: 897654321
Enter your name: athithi
Enter your phone number: 90213456
{'athithi': 90213456, 'kavin': 897654321, 'stevejobs': 99123456, 'hilary': 7689092341L}
1:Add a contact
2:Search a contact
3:Delete a contact
4:Update a contact
5:View directory
6:Exit
RESULT:
F. To write a python program to concatenate first two characters from two string and
reverse a string with string operation.
AIM: To write a python program to concatenate first two characters from two string and
reverse a string with string operation.
ALGORITHM:
STEP 1:Get two strings from the user .
STEP 2:Find a length of the both string .
STEP 3:Print the length of the both string .
STEP 4:Using “if else” condition, check whether the two strings are equal or not.
STEP 5:Concatenate first two characters of the first string and last two character of the
second string.
STEP 6:Print the concatenated string .
OUTPUT
RESULT: