0% found this document useful (0 votes)
12 views

Python and Java

Practical of Python and Java. And small details about python.

Uploaded by

robinhood5679
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python and Java

Practical of Python and Java. And small details about python.

Uploaded by

robinhood5679
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LIST OF PRACTICALS :-

1. Getting started with Python and IDLE in interactive and batch modes.

2. What do the following string methods do?

 lower
 count
 replace.

3. Write instructions to perform each of the steps below:

(a) Create a string containing at least five words and store it in a variable.

(b) Print out the string

(c) Convert the string to a list of words using the string split method.

(d) Sort the list into reverse alphabetical order using some of the list methods (you
might need to use dir(list) or help(list) to find appropriate methods).

(e) Print out the sorted, reversed list of words.

4. Write a program that determines whether the number is prime.

What is your favourite number? 27

27 is not prime.

What is your favourite number? 31

31 is prime.

5. Find all numbers which are multiple of 17, but not the multiple of 5, between 2000
and 2500?

6. Swap two integer numbers using a temporary variable. Repeat the exercise using the
code format: a, b=b, a.

Verify your results in both the cases.

7. Find the largest of n numbers, using a user defined function largest().

8. Write a function myReverse) which receives a string as an input and returns the
reverse of the string.

9. Check if a given string is palindrome or not.

10. WAP to convert Celsius to Fahrenheit.

11. Find the ASCIl value of charades.

12. WAP for simple calculator.


PRACTICAL :- 1

 Getting started with Python and IDLE in interactive and batch modes.

In Python, there are two options/methods for running code :

 Interactive mode
 Script mode/ Batch mode

Interactive Mode

Interactive mode, also known as the REPL provides us with a quick way of running
blocks or a single line of Python code. The code executes via the Python shell, which
comes with Python installation. Interactive mode is handy when you just want to
execute basic Python commands or you are new to Python programming and just want
to get your hands dirty with this beautiful language. To access the Python shell, open the
terminal of your operating system and then type “python".

Press the enter key and the Python shell will appear.

C:\Windows\system32>python Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015,


02:27:37) [MSC v.1900 64 bit(AMD64)] on wtn32 Type “help", “copyright", “credits” or
"license" for more information.

>>>

The >>> indicates that the Python shell is ready to execute and send your
commands to the Python interpreter. The result is immediately displayed on the Python
shell as soon as the Python interpreter interprets the command.

To run your Python statements, just type them and hit the enter key. You will get the
results immediately, unlike in script mode. For example, to print the text "Hello World",
we can type the following :

>>print(“Hello World”)

Hello World

>>>

Script Mode/ Batch mode

If you need to write a long piece of Python code or your Python script spans
multiple files, interactive mode is not recommended. Script mode is the way to go in
such cases. In script mode, you write your code in a text file then save it with a .py
extension which stands for “Python”. Note that you can use any text editor for this,
including Sublime, Code Block, notepad++, Dev C++ etc.

If you are in the standard Python shell, you can click “File'" then choose “New” or
simply hit “Ctrl +N” on your keyboard to open a blank script in which you can write your
code. You can then press “Ctrl + S” to save it.

Let us create a new file from the Python shell and give it the name "hello.py”. We need
to run the "Hello World” program. Add the following code to the file:

print("Hello World" )

Click “Run” then choose “Run Module”. This will run the Program:

Output :

 Key Di erences Between Interactive and Script Mode

Here are the key di erences between programming in interactive mode and
programming in script mode:
1. In script mode, a file must be created and saved before executing the
code to get results. In interactive mode, the result is returned
immediately after pressing the enter key.

2. In script mode, you are provided with a direct way of editing your code.
This is not possible in interactive mode.

 Conclusion

There are two modes through which we can create and run Python scripts:
interactive mode and script mode. The interactive mode involves running your
codes directly on the Python shell which can be accessed from the terminal of
the operating system. In the script mode, you have to create a file, give it a name
with a .py the extension then runs your code. The interactive mode is suitable
when running a few lines of code. The script mode is recommended when you
need to create large applications.
PRACTICAL :- 2

 What do the following string methods do?


 lower
 count
 replace

lower which is written as lower() is a method in string which returns the


lowercased string from the given string. It converts all uppercase characters to
lowercase.

If no uppercase characters exist, it returns the original string.

Example:

>>>name= “YASH RAJ”


>>> name.lower()
“yash raj”

count which is written as count() is a method in string which returns the number of
occurrences of a substring in the given string. In simple words, count() method
searches the substring in the given string and returns how many times the substring is
present in it.

Example:

>> name= "YASH RAJ"


>>> name.count ('A')
2

replace which is written as replace() is a method in string which returns a copy of the
string where all occurrences of a substring is replaced with another substring.

old - old substring you want to replace.

new - new substring which would replace the old substring.

Example:

>> name "HELLO! YASH RAJ"


name.replace(‘HELLO’, ‘HI’)
HI! YASH RAJ
PRACTICAL:- 3

 Write instructions to perform each of the steps below:

(a) Create a string containing at least five words and store it in a variable.

(b) Print out the string.

(c) Convert the string to a list of words using the string split method.

(d) Sort the list into reverse alphabetical order using some of the list methods
(you might need to use dir(list) or help(list) to find appropriate methods).

(e) Print out the sorted, reversed list of words.

name= 'Hello world my name is Yash Raj’


print(name)
myName=name.split()
myName.sort(reverse=True )
print(myName)
PRACTICAL:- 4

 Write a program that determines whether the number is prime:

What is your favourite number? 27

27 is not prime.

What is your favourite number? 31

31 is prime.

a=int(input("What is your favourite number?"))

i=2

while(i <= a/2):

if (a%i==0):

print(a, “is not prime” )

break

I = i+1

else:

print(a, 'is prime')


PRACTICAL :-5

 Find all numbers which are multiple of 17, but not the multiple of 5, between
2000 and 2500?

for a in range (2000, 2500):

if(a%17==0 and a%5 ! =0):

print(a)
PRACTICAL:- 6

 Swap two integer numbers using a temporary variable. Repeat the exercise using
the code format: a, b = b, a. Verify your results in both the cases.

a, b=10, 5

temp = a

a=b

b = temp

print(b, a)

a, b=10, 5
a, b=b, a

print(a, b)

print('Both the cases are verified' )


PRACTICAL:- 7

 Wite a function myReverse() which receives a string as an input and returns the
reverse of the string.

def myReverse() :
a=input('Enter the string:’)
b=a[::-1]
return b
myReverse()

OUTPUT :

Enter the string: yash


hsay
>>>

You might also like