OOP Lab 1
OOP Lab 1
Objective
To get introduced with fundamentals of python.
Theory
Introduction of Programming
Programming is the process of taking an algorithm and encoding it into a notation, a
programming language, so that it can be executed by a computer. Although many programming
languages and many different types of computers exist, the important first step is the need to
have the solution. Without an algorithm there can be no program.
Computer science is not the study of programming. Programming, however, is an important part
of what a computer scientist does. Programming is often the way that we create a representation
for our solutions. Therefore, this language representation and the process of creating it becomes a
fundamental part of the discipline.
Algorithms describe the solution to a problem in terms of the data needed to represent the
problem instance and the set of steps necessary to produce the intended result. Programming
languages must provide a notational way to represent both the process and the data. To this end,
languages provide control constructs and data types.
Source Code:
Source code is the fundamental component of a computer program that is created by a
programmer. It can be read and easily understood by a human being. When a programmer types
a sequence of C language statements into Windows Notepad, for example, and saves the
sequence as a text file, the text file is said to contain the source code.
Page | 1
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Object Code:
Object Code is in the form of Binary Numbers. It is in Machine Readable. And this type of Code
is generated by Compiler. It is Output of Compiler.
What is Python?
Virtually all modern programming languages make us of an IDE, or Integrated Development
Environment, which allows the creation, editing, testing, and saving of programs and modules. In
Python, the IDE is called IDLE (like many items in the language, this is a reference to the British
comedy group Monty Python, and in this case, one of its members, Eric Idle). Before opening
IDLE, it is worth recalling that there are three basic types of simple variables in Python: integers
(whole numbers, commonly used as an index), floats (that is, numbers with a decimal point,
AKA real numbers), and strings (collections of alphanumeric characters such as names,
sentences, or numbers that are not manipulated mathematically such as a part number or zip
code). A legal variable name must start with a letter. It is then optionally followed by some
collection of letters, numerals and the underscore. It cannot contain any other characters or
spaces, and cannot be a reserved word (i.e., a word with a special meaning in the language such
as a command or operator). In Python, variables may be created by simply declaring them and
assigning a value to them.
Examples include:
a=2.3
name=”Joe”
It is best to think of the equal sign as “gets”. That is, think of the first example as “the variable
agets the floating point value 2.3” and the second as “the variable name gets the string Joe”. An
assignment command such as these literally reserves space in the computer’s memory for the
variable and tags it with the variable name. Then, it stores the appropriate value at that location
for future use.
Page | 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
The >>> should reappear. This command defines a variable called ‘a’ and copies the integer
value5 into it. In similar manner, type in the following commands:
b=13
x=5.0
y=13.0
m=”Mary”
n=”Nancy”
It is very important that the “.0” portions be included. This is how integers and floats are
distinguished: floats always have a decimal point, integers don’t. Also, it is possible to define the
strings using the apostrophe‘ versus the quote ”. This can be handy if you need to have a string
that includes a quote or apostrophe within it; merely define the string with the other character. In
any case, the computer’s memory now looks something like this
The trick now, of course, is to access these values, manipulate them, and see the results. An
important command for this process is the print command. print will print what follows it, either
variables or expressions, on to the output window. Note that like all built-in commands and
Page | 3
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
functions in Python, this command is all lower case. Capitalizing it will generate an error. Also,
note that commands will be color coded orange-red. At the prompt, type the following:
Example # 2
print( a )
Output:
Example # 3
Output:
Page | 4
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Continue with the following expression:
print( a + b )
This results in the value 18. This line retrieves the values of a and b from memory, adds them
together, and prints the result on the output window. Neither a nor b are altered in the process.
Alternately, we could have created a brand new variable and printed it. The result will be the
same. Enter the following two lines to verify this:
c = a + b
print( c )
The only difference is that this version adds a new “slot” called c to the memory map above. It
isworth noting that once a variable is created, its value may be recomputed over and over if
desired. For example, type the following:
c = 20 + a
print( c )
Example # 4
Page | 5
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
As useful as the output shell window is, you will not be able to easily edit and save programs
from it. For this, you’ll need an editor window. From the File menu of the output window select
New File. A second window will pop open. It will not contain a cursor. This is a simple text
editor. It will not interpret lines as you type them. The edit window is where you will normally
write your programs (occasionally going back to the output to see results or to use its “scratch
pad” feature).
One of the most useful operators in Python is #, which is used for comments. That is, Python will
ignore anything on a line that follows this symbol; it’s for human consumption only. This is how
you can place documentation inside a program. By doing so, the code and documentation can
never get separated. Type in the following two line program
Page | 6
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
The editor works like any other text editor that you might have used. That is, you can insert, delete, cut,
copy, paste, etc. Unlike a word processor, there is no selection for font, margins, and the like. After all,
the point is to write down commands. Python and the computer don’t care how pretty those lines
appear.
To run the program, go to the Run menu and select Run Module. You will be prompted to save the
program. NEVER save your code to the hard drive on a lab computer. ONLY save to either your student
account space (H drive) or to an external USB drive. It is suggested that you create a folder on your
student account for Python programs and store everything there, using a USB drive as a backup.
When naming a Python program, a .py extension must be used. Failing to do so will result in code that
will not be recognized by the system as a Python program. For this exercise, it is suggested that the
program be saved as hello.py
After the filename is entered, select Save. Python will now load your code and start executing it (i.e.,
performing the commands you entered). Move back to the output window. You should see the
following at the bottom:
Hello World!
Example # 5
Output:
Page | 7
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Lab Task
1.1 write a program in python to add, subtract, multiply and divide 5 numbers.
1.2 write a program in python to display a data of your in Nadra using editor window procedure.
NOTE: Attach printouts of above mentioned task with your name and roll number in header or
footer.
Learning outcomes:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
Page | 8