ASSIGNMENT NO: 3
AIM : To accept an object mass in kilograms and velocity in
meters per second and display its
momentum. Momentum is calculated as e=mc2 where m is the
mass of the object and c is its
velocity
PROBLEM STATEMENT : To calculate momentum for
given object
PREREQUISITES:
1. Knowledge of python programming
COURSE OBJECTIVE:
2. Understand basic concepts of programming language
THEORY:
Momentum is the quantity of motion of a moving body. In a
basic sense, the more momentum a
moving object has, the harder it is to stop. In this assignment,
user has to accept mass ofobject in Kg
and velocity in M/Sec to display its momentum.
ALGORITHM:
1. Accept mass of object in Kg from user
2. Accept velocity in M/Sec from user
3. Calculate e=mc2
4. Print momentum
FAQ’s:
1. Why Python is versatile?
2. Which operator in python is use to print power of a number ?
CONCLUSION: Outcome of the experiment is :
Students are able to implement python program to accept an
object mass in kilograms and velocity
in meters per second and display its momentum.
ASSIGNMENT NO: 8
AIM: To study various String Operations.
PROBLEM STATEMENT: Write a python program that
accepts a string from user and perform
following string operations- i. Calculate length of string ii.
String reversal iii. Equality check of
two strings iii. Check palindrome ii. Check substring
COURSE OBJECTIVE: To understand how to implement
various operations on Strings in Python.
COURSE OUTCOME: Able to implement Strings and its
various manipulation operations in
Python
THEORY:
How T o Create Strings In Python?
Creating strings is easy as you only need to enclose the
characters either in single or double-quotes.
Example:
# Python string examples - all assignments are identical.
String_var = 'Python'
String_var = "Python"
String_var = """Python"""
# with T riple quotes Strings can extend to multiple lines
String_var = """ T his document will help you to
explore all the concepts
of Python Strings!!! """
# Replace "document" with "tutorial" and store in another
variable
print (substr_var)
Index And Slice Strings In Python
We need to know the index of a character to retrieve it from the
String.Like the most
programming languages, Python allows to index from the zeroth
position in Strings. But it also
supports negative indexes. Index of ‘-1’ represents the last
character ofthe String. Similarly using ‘-2’,
we can access the penultimate element of the string and so on.
sample_str = 'Python String'
print (sample_str[0]) # return 1st character
# output: P
print (sample_str[-1])
print (sample_str[-2]) # return last second character
# output: n
Slice A String In Python
To retrieve a range of characters in a String, we use ‘slicing
operator,’ the colon ‘:’ sign. W ith the
slicing operator, we define the range as [a:b]. It’ll let us print all
the characters of the String
starting from index ‘a’up to char at index ‘b-1’. So the char at
index ‘b’is not a part of the output
sample_str = 'Python String'
print (sample_str[3:5])
#return a range of character # ho
print (sample_str[7:]) # return all characters from index 7
print (sample_str[:6]) # return all characters before index 6
print (sample_str[7:-4])
# St
Operations on Strings:
1.Calculate length of String
In python len() function is used to calculate length of a string in
Python.
E.g:
# Python program to demonstrate the use of len() method
# Length of below string is 5
string = "ZCOER"
print(len(string))
# Length of below string is 15
string = "My college name is ZCOER"
print(len(string))
2. String Reversal
def reverse(s):
str = ""
for i in s:
str = i + str
return str
3. Equality check of two Strings
string1 = "sample"
string2 = "sample"
if string1 == string2 :
print("Strings are equal with text : ", string1," & ",string2)
else :
print ("Strings are not equal")
4. Check Palindrome
# Python program to check if a string is palindrome or not
x = "malayalam"
w = ""
for i in x:
w=i+w
if (x==w):
print("YES")
5. Check Substring
# function to check if small string is there in big string
def check(string, sub_str):
if (string.find(sub_str) == -1):
print("NO")
else:
print("YES")
# driver code
string = "First Y ear Engineering"
sub_str ="First"
check(string, sub_str)
CONCLUSION: Outcome of experiment is :Students are able
to implement string
Operations successfully.
FAQ’s:
1. Write a short note on format operator.
2. What is slice operation? Explain with an example.
3. How to concatenate , append and multiplication in strings?