Docsity

Advertisement for security, Assignments of Mathematical logic

Toggle sidebar
bg1
Preston University Kohat
Islamabad Campus
PROGRAMMING FUNDAMENTALS
PYTHON PROGRAMMING LANGUAGE
LAB MANUAL
For the Students of First Semester BSCS & MCS Programs
PART - II
BY
MS. MUNAZZA MAH JABEEN
ASSISTANT PROFESSOR – COMPUTER SCIENCE
DEPARTMENT OF COMPUTER SCIENCE
PRESTON UNIVERSITY KOHAT, ISLAMABAD CAMPUS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Advertisement for security and more Assignments Mathematical logic in PDF only on Docsity!

Preston University Kohat

Islamabad Campus

PROGRAMMING FUNDAMENTALS

PYTHON PROGRAMMING LANGUAGE

LAB MANUAL

For the Students of First Semester BSCS & MCS Programs

PART - II

BY

MS. MUNAZZA MAH JABEEN

ASSISTANT PROFESSOR – COMPUTER SCIENCE

DEPARTMENT OF COMPUTER SCIENCE

PRESTON UNIVERSITY KOHAT, ISLAMABAD CAMPUS

TABLE OF CONTENTS:

OBJECTIVES

  1. To introduce students to the basic knowledge of programming fundamentals of C & Python language.
  2. To impart writing skill of C & Python programming to the students and solving problems.
  3. To impart the concepts like looping, array, functions, pointers, file, structure.

COURSE OUTCOME

After completing this lab course you will be able to:

  1. Understand the logic for a given problem.
  2. Write the algorithm of a given problem.
  3. Draw a flow chart of a given problem.
  4. Recognize and understand the syntax and construction of Python programming code.
  5. Gain experience of procedural language programming.
  6. Know the steps involved in debugging Python code.
  7. Learn the methods of iteration or looping and branching.
  8. Python has implicit support for Data Structures which enable you to store and access data. These structures are called List, Dictionary, Tuple and Set.
  9. Python allows its users to create their own Data Structures enabling them to have full control over their functionality. The most prominent Data Structures are Stack, Queue, Tree, and Linked List and so on which are also available to you in other programming languages.
  10. Understand how to access and use library functions.
  11. Understand function declaration and definition.
  12. Understand proper use of user defined functions.
  13. Write programs to print output on the screen as well as in the files.
  14. Apply all the concepts that have been covered in the theory course, and
  15. Know the alternative ways of providing solution to a given problem.

INTRODUCTION ABOUT LAB

Steps involved in program development:-

To develop the program in high level language and translate it into machine level language following steps have to be practiced.

  1. Writing and editing the program.
  2. Linking the program with the required library modules.
  3. Compiling the program.
  4. Executing the program.

Algorithm:-

It is a method of representing the step by step process for solving a problem. Each step is called an instruction.

Characteristics of algorithm are:

  1. Finiteness: - It terminates with finite number of steps.
  2. Definiteness: - Each step of algorithm is exactly defined.
  3. Effectiveness: - All the operations used in the algorithm can be performed exactly in a fixed duration of time.
  4. Input: - An algorithm must have an input before the execution of program begins.
  5. Output: - An algorithm has one or more outputs after the execution of the program.

Example of algorithm to find sum of two numbers:

Step1: BEGIN Step2: READ a, b Step3: ADD a and b and store in variable c Step4: DISPLAY c Step5: STOP

Hardware Requirement: Desktop Computer / laptop computer.

Software Requirement: Windows 7 or later, mac OS, and Linux.

Windows

The latest version of Windows is Windows 10. Generally, you should be on the latest version of the OS, but if you have an older machine or other software requires it, you might be stuck with Windows 7 or older. You should be fine with both, but be aware that if you want to use Python 3.5 or above, Windows XP is not supported. If you go for Windows, there are some shell options for you. Windows comes with the Command line, but also PowerShell that feels more like a normal terminal you would know from the Linux- or Mac world. A newcomer that is even better is Windows Terminal. This highly customizable terminal is a blast to use.

Linux

Linux Linux is not really the operating system, it’s a kernel. What you would consider an OS is the distributions/distros. One of the benefits of using Linux is that these distros are lightweight and super customizable. You can shop around and find the one that fits you perfectly. Some popular distros are Ubuntu, Mint, Fedora, and CentOS. Linux is great for development. It comes with a terminal as well and a lot of the text editors and IDEs work on Linux. Linux is open-source so it is a budget- friendly alternative.

Python

Of course, you need Python. Head over to python.org, download the installer, and run it. If you write “python3” in a terminal (if you downloaded a python3 version) you should see it execute and print what python version you are running.

Text Editors and IDEs

There are many text editors out there. Some things you are looking for in a text editor is: Syntax highlighting: Python is a popular language so most text editors have this as standard or as a downloadable extension. Code prediction: If you have made a variable called ninja_turtles, it is nice that the editor will predict your variable name after you start typing ninja… Having the software look for modules while you type can be used for quick coding and learning.

Tab indent: Most editors allow you to choose how many spaces you want in a tab. Python’s PEP8 (the holy bible) states you should be using 4 spaces. Python 3 even disallows mixing tabs and spaces. Few Common ones are: PyCharm, Atom, Thonny, Pydroid (For running Python Code on Android). You can also Program online.

STRUCTURE OF PYTHON PROGRAM:

OPERATORS

Python supports a rich set of operators, which are symbols used within an expression to specify the manipulations to be performed while evaluating that expression. Python has the following operators:

  1. Arithmetic: **+, -, *, /, %, , // 2. Assignment: **=, +=, - =, *=, /=, %=, &=, |=, //=, =, ^=, <<=, >>= 3. Comparison: ==, !=, >, >=, <, <=
  2. Logical: and, or, not 5. Identity operators: is, is not 6. Membership operators: in, not in
  3. Bitwise operators: &, |, ^, ~, <<, >>

SALIENT FEATURES OF PYTHON Python language has some characteristics that define the language and also have led to its popularity as a programming language. Easy to Learn and Use: Python is easy to learn and use. It is developer-friendly and high level programming language. Expressive Language: Python language is more expressive means that it is more understandable and readable. Interpreted Language: Python is an interpreted language i.e. interpreter executes the code line by line at a time. This makes debugging easy and thus suitable for beginners. Cross-platform Language: Python can run equally on different platforms such as Windows, Linux, UNIX and Macintosh etc. So, we can say that Python is a portable language. Dynamically Typed language: it is dynamically-typed as variable type is checked during run-time when they are bound to the object with possibility of binding the same variables with different type object during program execution leading to less optimized code avoiding run-time errors. Free and Open Source: Python language is freely available at official web address. The source-code is also available. Therefore it is open source. Object-Oriented Language: Python supports object oriented language and concepts of classes and objects come into existence. Extensible: It implies that other languages such as C/C++ can be used to compile the code and thus it can be used further in our python code. Large Standard Library: Python has a large and broad library and provides rich set of module and functions for rapid application development. GUI Programming Support: Graphical user interfaces can be developed using Python. Integrated: It can be easily integrated with languages like C, C++, and JAVA etc.

INSTRUCTIONS TO STUDENTS

FOR PREPARING PROGRAMMING IN PYTHON LAB REPORT

This Lab Manual is prepared to help the students with their practical understanding and development of programming skills, and may be used as a base reference during the lab/practical classes.

Students have to submit Lab Exercise report of previous lab into corresponding next lab, and can be collected back after it has been checked and signed. At the end of the semester, students should compile all the Lab Exercise reports into a single report and submit during the end semester sessional examination.

“ Sample of Lab report” is shown for LAB Exercise #1 in this manual. For the

rest of the labs, the reporting style as provided is to be followed. The lab report to

be submitted during the end semester Sessional Examination should include at

least the following topics:-

  1. Top Cover page – pg. 12 ( to be used while compiling all the Lab Exercise reports into single report )
  2. Index – pg. 13 ( to be used while compiling all the Lab Exercise reports into single report )
  3. Cover page – pg. 14 ( to be attached with every Lab Exercise )
  4. Title of the program
  5. Algorithm ( optional )
  6. Flowchart ( optional )
  7. Coding
  8. Output (compilation, debugging & testing)

Students are to show the coding and output to the instructor/course coordinator for the additional lab exercises given in each lab module.

Note: The lab exercises may not be completed in a single specific lab. Students are

encouraged to complete the programming questions given in the exercise prior to

come to the lab hour and do the lab for the given programs.

INDEX:

  • OBJECTIVES
  • COURSE OUTCOME
  • INTRODUCTION ABOUT LAB.......................................................................
  • ABOUT PYTHON LANGUAGE
  • HISTORY OF PYTHON
  • STRUCTURE OF PYTHON PROGRAM:
  • KEYWORDS
  • OPERATORS
  • SALIENT FEATURES OF PYTHON
  • INSTRUCTIONS TO STUDENTS
  • PROGRAMMING IN PYTHON LAB REPORT
  • LAB Sheet #1
  • LAB Sheet #2
  • LAB Sheet #3
  • LAB Sheet #4
  • LAB Sheet #5
  • LAB Sheet #6
  • LAB Sheet #7
  • PROGRAMMING IN PYTHON LAB REPORT
  • LAB Sheet #1
  • LAB Sheet #2
  • LAB Sheet #3
  • LAB Sheet #4
  • LAB Sheet #5
  • LAB Sheet #6
  • LAB Sheet #7

Preston University Kohat Islamabad Campus Department of Computer Science

LAB Sheet #

Programming Fundamentals Lab Report Submitted By:

Name: ___________________

Reg. No: ___________________

Submitted To:

Department of Computer Science

Lab Date: Marks & Signature

Submission Date:

Flowchart:

Code:

Variables are created here by initialization

h = 10 w = 12 d = 8

Cube Volume is calculated here

vol = h * w * d

calculated volume is displayed here

print("The Volume of the cube is:", vol)

Output (Compilation, Debugging & Testing)

The Volume of the cube is: 960

Discussion and Conclusion

This is the first code written in Python program. The program is focused on the

calculation of volume of a cube for the given height, width and depth. From this

lab, I understood the basic structure of Python programming including the meaning of steps to solve the problem. Hence, volume of a cube is calculated and

displayed.

Lab exercises (please code yourself and show the output to instructor):

1. Personal Information: Write a program that displays the following information: - Your name - Your address, with city, state, and ZIP - Your telephone number - Your college major 2. Sales Prediction A company has determined that its annual profit is typically 23 percent of total sales. Write a program that asks the user to enter the projected amount of total sales, then displays the profit that will be made from that amount. **Hint: Use the value 0.23 to represent 23 percent.

  1. Pounds to Kilograms** One pound is equivalent to 0.454 kilograms. Write a program that asks the user to enter the mass of an object in pounds and then calculates and displays the mass of the object in kilograms. 4. Total Purchase A customer in a store is purchasing five items. Write a program that asks for the price of each item, then displays the subtotal of the sale, the amount of sales tax, and the total. Assume the sales tax is 7 percent. 5. Distance Traveled Assuming there are no accidents or delays, the distance that a car travels down the interstate can be calculated with the following formula: Distance = Speed x Time A car is traveling at 70 miles per hour. Write a program that displays the following:  The distance the car will travel in 6 hours  The distance the car will travel in 10 hours  The distance the car will travel in 15 hours

Objective(s):

To be familiar with decision structure and Boolean logic in Python.

Title:

1. Number Analyzer Write a program that asks the user to enter an integer. The program should display “Positive” if the number is greater than 0, “Negative” if the number is less than 0, and “Zero” if the number is equal to 0. The program should then display “Even” if the number is even, and “Odd” if the number is odd. 2. Areas of Rectangles The area of a rectangle is the rectangle’s length times its width. Write a program that asks for the length and width of two rectangles. The program should tell the user which rectangle has the greater area, or if the areas are the same. 3. Quarter of the Year Write a program that asks the user for a month as a number between 1 and 12. The program should display a message indicating whether the month is in the first quarter, the second quarter, the third quarter, or the fourth quarter of the year. Following are the guidelines:  If the user enters either 1, 2, or 3, the month is in the first quarter.  If the user enters a number between 4 and 6, the month is in the second quarter.  If the number is either 7, 8, or 9, the month is in the third quarter.  If the month is between 10 and 12, the month is in the fourth quarter.  If the number is not between 1 and 12, the program should display an error. 4. Mass and Weight Scientists measure an object’s mass in kilograms and its weight in newtons. If you know the amount of mass of an object in kilograms, you can calculate its weight in newtons with the following formula: Weight = mass x 9. Write a program that asks the user to enter an object’s mass, then calculates its weight. If the object weighs more than 500 newtons, display a message indicating that it is too heavy. If the object weighs less than 100 newtons, display a message indicating that it is too light.

5. Roman Numerals Write a program that prompts the user to enter a number within the range of 1 through 10. The program should display the Roman numeral version of that number. If the number is outside the range of 1 through 10, the program should display an error message. The following table shows the Roman numerals for the numbers 1 through 10:

Number Roman Numeral 1 I 2 II 3 III 4 IV 5 V 6 VI 7 VII 8 VIII 9 IX 10 X