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

Lab 04

Uploaded by

abdulmoeez70899
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab 04

Uploaded by

abdulmoeez70899
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

DEPARTMENT OF COMPUTER & SOFTWARE ENGINEERING

COLLEGE OF E&ME, NUST, RAWALPINDI

EC- xxx Application of ICT

LAB MANUAL – 04

Course Instructor:

Lab Engineer: Kashaf Raheem

Student Name: _______________________________________________

Degree/ Syndicate: ____________________________________________


LAB # 4: INTRODUCTION TO PROGRAMMING (PYTHON)

Lab Objective:
● To familiarize the students with basic programming concepts.

Hardware/Software required:
Hardware: Desktop/ Notebook Computer
Software Tool: Online Notebook

Lab Description:

Python is a high-level general-purpose programming language. Because code is automatically


compiled to byte code and executed, Python is suitable for use as a scripting language, Web
application implementation language, etc. Because Python can be extended in C and C++,
Python can provide the speed needed for even compute intensive tasks.
A sample code in python:

Interactive Python:
If you execute Python from the command line with no script, Python gives you an interactive
prompt. This is an excellent facility for learning Python and for trying small snippets of code.
Many of the examples that follow were developed using the Python interactive prompt.
In addition, there are tools that will give you a more powerful and fancy Python interactive
mode. One example is IPython, which is available at http://ipython.scipy.org/. You may also
want to consider using IDLE. IDLE is a graphical integrated development environment for
Python. It contains a Python shell which helps you code in python by Auto-indent, Debugging
and Color-coding your program
1. Installing Python:
Download Python 3.6.2 for Windows from the following link:
https://www.python.org/downloads/
Install the downloaded executable file.
2. Opening IDLE
After installing Python, go to the start menu and search Python. Run the program labeled as
Integrated Development Environment (IDLE).
Mathematical Operations in Python:
>>> 1 + 1
2
>>> 6-5
1
>>> 2*5
10
>>> 5**2
25
>>> 21/3
7
>>> 23/3
7
>>> 23.0/3.0
7.6666...
>>> 23%3
2
Operators in Python

Command Name Example Output

+ Addition 4+5 9

- Subtraction 8-5 3

* Multiplication 4*5 20

/ Division 19/3 6

% Remainder 19%3 5

** Exponent 2**4 16
Operator Precedence in Python:

Operators Descriptions

() Parentheses

** Exponent (Raise to the power)

* / % // Multiplication, Divide, Reminder, Floor Division

+ - Addition, Subtraction

>> << Right and Left Bitwise Shift

Comments in Python:
>>> #Commented Code

Variables:
Variables are containers for storing data values. In Python, it is not needed to declare variables
by specific data type. The Data type of a variable is decided at run-time.

type () keyword tells the type of argument.


Variable Names
● Must start with underscore or with a letter.

● Cannot start with a number.

● Can contain underscores and alpha –numeric letters.

● Variables names are case-sensitive


Strings:
Strings are represented either by using double or single quotes.
Python Example:
word1 = "Good"
word2 = "Morning"
word3 = "to you too!"
print (word1, word2)
sentence = word1 + " " + word2 + " " +word3
print (sentence)
print(word1[2])

Boolean Operators in Python:

Boolean operators return either TRUE or FALSE

Expression Function

< less than

<= less that or equal to

> greater than

>= greater than or equal to

!= not equal to

<> not equal to (alternate)

== equal to

Conditional Statements:
‘if - else' - Statement
a=1
if a > 5:
print ("This shouldn't happen.") #condition true
else:
print ("This should happen.") #condition false
‘elif' - Statement
z=4
if z > 70:
print ("Something is very wrong")
elif z < 7:
print ("This is normal")

Input from user:


input(“ ”):
a = input (“Enter Value for variable a: ”)
print (a)
input: Reads a string of text from user input.
Example:
name = input (“What's your name Lad? ")
print (name, "... what a nice name!")
Output:
What is your name Lad? Ali
Ali... what a nice name!

Typecasting input
# type cast into integer
input_a = int(input_a)
String Properties:
len(string) - number of characters in a string (including spaces)
str.lower(string) - lowercase version of a string
str.upper(string) - uppercase version of a string
Example:
name = "Linkin Park"
length = len(name)
big_name = str.upper(name)
print (big_name, "has", length, "characters")
Output:
LINKIN PARK has 11 characters

Loops in Python:
'while' loop
a=0
while a < 10:
a=a+1
print (a)
'for' loop
for i in range(1, 5):
print (i)
for i in range(1, 5):
print (i)
else:
print ('The for loop is over')

Lab Tasks:
1. Write a python code to display your name, roll no, age, etc on output.
2. Modify task01 and take input from user in the variables name, roll no, and age and
display on output.
3. Write a python code to check whether a number is even or odd.

You might also like