PRACTICAL 2: Hand on with Python
PRACTICAL
2: Hand on with Python
Python is an interpreted
high-level programming language for general-purpose programming. Created by
Guido van Rossum and first released in 1991, Python has a design philosophy
that emphasizes code readability, and a syntax that allows programmers to
express concepts in fewer lines of code.
Python interpreters are
available for many operating systems. Python, the reference implementation of
Python, is open source software and has a community-based development model, as
do nearly all of its variant implementations. Python is managed by the
non-profit Python Software Foundation.
INSTALLING
AND DOWNLOADING (Python: Version 3.6.2)
The Python download
requires about 30 Mb of disk space; keep it on your machine, in case you need
to re-install Python. When installed, Python requires about an additional 90 Mb
of disk space.
The
following page will appear in your browser.
Figure 10-
python.org
2.
Click the Download Python 3.6.2 button.
The file named python-3.6.2.exe should start downloading into your standard
download folder. This file is about 30 Mb so it might take a while to download
fully if you are on a slow internet connection (it took me about 10 seconds
over a cable modem).
3.
Move this file to a more permanent
location, so that you can install Python (and reinstall it easily later, if
necessary.
4.
Double-click the icon labeling the file python-3.6.2.exe. An Open File - Security Warning
pop-up window will appear.
Figure 11- security
warning
5.
Click Run. A Python 3.6.2 (32-bit) Setup
pop-up window will appear. Ensure that the Install launcher for all users
(recommended) and the Add Python 3.6 to PATH checkboxes at the bottom are checked.
If the Python Installer finds an earlier version of Python installed on your
computer, the Install Now message will instead appear as Upgrade Now (and the
checkboxes will not appear).
Figure 12-setup window
6.
A new Python 3.6.2 (32-bit) Setup pop-up
window will appear with a Setup Progress message and a progress bar. During
installation, it will show the various components it is installing and move the
progress bar towards completion. Soon, a new Python 3.6.2 (32-bit) Setup pop-up
window will appear with a Setup was successfuly message.
Figure 13-progress window
7. Click the close button. Python
should now be installed.
BASICS
OF PYTHON
1.
ARITHMATIC OPERATION: We can simply add
two numbers and we can get the output in the next line.
Figure 14-arithmatic operations
2.
VARIABLE ASSIGNMENT: Variables are nothing
but reserved memory locations to store values. This means that when you create
a variable you reserve some space in memory. Based on the data type of a
variable, the interpreter allocates memory and decides what can be stored in
the reserved memory.
Figure 15-variable assignment
3.
STRINGS: Python
does not support a character type; these are treated as strings of length one,
thus also considered a substring.
Figure 16-Strings
4.
PRINTING: We can print the desired by
using print keyword followed by ( ).
Figure 17- Printing
File
I/O with Python
File I/O mainly deals
with saving a file to our local hard disk and reading a file and reason why
this is so important because when you create a program and all the objects and
variable we create in our program to store information is kind of actually stored
in RAM of our system and it’s a kind of short term memory, as we exit out of
that program all the information gets discarded from the RAM.
So one of the main reason
we are dealing with I/O files is saving the file in our local hard disk and use
it any time when we want.
Writing
to a File
·
Creating a file object (here
named outfile).
·
Using open( ) method . It creates the file
object.
·
Giving the file name with its proper
extension (here named output.txt).
·
Using wt
as syntax for write mode to write a text file.
·
Using write( ) method to write into the
file.
·
Using the close( ) method to close the
file
·
The file have been saved to local memory.
·
Now the file is physically stored as a txt
file
Screen shots of the above
steps are shown below
Figure 18-Writing to a file
Figure 19-file saved in
c:\\python36-32
Figure 20-
txt file saved
Reading
form a File
·
Creating a file object (here
named outfile).
·
Using open( ) method . It creates the file
object.
·
Giving the file name with its proper
extension (here named output.txt).
·
Using rt
as syntax for read mode to read a text file.
·
Using read( ) method to read from the
file.
·
Using the close( ) method to close the
file
Figure 21-reading
form the file
Comments
Post a Comment