Python and Java
Python and Java
1. Getting started with Python and IDLE in interactive and batch modes.
lower
count
replace.
(a) Create a string containing at least five words and store it in a variable.
(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).
27 is not prime.
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.
8. Write a function myReverse) which receives a string as an input and returns the
reverse of the string.
Getting started with Python and IDLE in interactive and batch modes.
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.
>>>
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
>>>
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 :
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
Example:
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:
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.
Example:
(a) Create a string containing at least five words and store it in a variable.
(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).
27 is not prime.
31 is prime.
i=2
if (a%i==0):
break
I = i+1
else:
Find all numbers which are multiple of 17, but not the multiple of 5, between
2000 and 2500?
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)
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 :