Python Language Programming Lab - Prudentac

3y ago
87 Views
6 Downloads
906.63 KB
53 Pages
Last View : 2d ago
Last Download : 2m ago
Upload by : Noelle Grant
Transcription

LABORATORY MANUALPython Language Programming LabDepartment of Computer Science and Engineering

LIST OF EXPERIMENTSExp.No.CorrespondingTitle of experimentCO1.Demonstrate the working of ‘id’ and ‘type’ functions.C 220.12.Write a Python program to find all prime numbers within a givenrangeC 220.13.Write a Python program to print ‘n terms of Fibonacci seriesusing iterationWrite a Python program demonstrate use of slicing in string.C 220.14.5.6.7.8.C 220.1Write a Python programa) To add 'ing' at the end of a given string (length should beat least 3). If the given string already ends with 'ing'then add 'ly' instead. If the string length of the given stringis less than 3, leave it unchanged. Sample String : 'abc'Expected Result : 'abcing' Sample String : 'string'Expected Result : 'stringly'b) To get a string from a given string where all occurrencesof its first char have been changed to ' ', except the firstchar itself.C 220.2Write a Python program toa) Compute the frequency of the words from the input. Theoutput should output after sorting the keyalphanumerically.b) program that accepts a comma separated sequence ofwords as input and prints the words in a comma-separatedsequence after sorting them alphabetically.C 220.2Write a Python program that accepts a sequence of whitespaceseparated words as input and prints the words after removing allduplicate words and sorting them alphanumericallyC 220.2Write a Python program to demonstrate use of list & relatedfunctionsC 220.29.Write a Python program to demonstrate use Dictionary& relatedfunctions.C 220.210.Write a Python program to demonstrate use tuple, set & relatedfunctions.C 220.3Write a Python program to implement stack using list.11.PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualC 220.3Page 6

Write a Python program to implement queue using list.C 220.312.13.14.Write a Python program to read and write from a file.Write a Python program copy a file.Write a Python program to demonstrate working of classes and15. objects.Write a Python program to demonstrate class method & static16. method.17.18.Write a Python program to demonstrate constructors.Write a Python program to demonstrate inheritance.Write a Python program to demonstrate19. aggregation/compositionsWrite a Python program to create a small GUI application for20. insert, update and delete in a table using Oracle as backend andfront end for creating formPYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualC 220.3C 220.3C 220.3C 220.4C 220.4C 220.4C 220.4C 220.4Page 8

Department of Computer Science & EngineeringContent Beyond Syllabus21. Write a Python program to compute area and circumference of aTriangle. Take input from user.C 220.222. Write a program to check if a number is Odd or even. Take inputFrom user.C 220.223. Write a program to check that a given year is Leap Year or not.C 220.2PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 9

Department of Computer Science & EngineeringINTRODUCTIONPython is a language with a simple syntax, and a powerful set of libraries. It is an interpretedlanguage, with a rich programming environment, including a robust debugger and profiler. Whileit is easy for beginners to learn, it is widely used in many scientific areas for data exploration.This course is an introduction to the Python programming language for students without priorprogramming experience. We cover data types, control flow, object-oriented programming, andgraphical user interface-driven applications. The examples and problems used in this course aredrawn from diverse areas such as text processing, simple graphics creation and imagemanipulation, HTML and web programming, and genomics.Scope & Objective:1. Learn basic programming constructs –data types, decision structures, control structures inpython2. Know how to use libraries for string manipulation and user-defined functions.3. Learn to use in-built data structures in python – Lists, Tuples, Dictionary andFile handling.4. Learn the fundamental principles of Object-Oriented Programming5. Solve problems through application of OO concepts and using Files/databaseUse Python Shell (using command line) and IDLE – Interactive development environment. To evaluate expression. To create a script. Using IDLEIDLE is the standard Python development environment. Its name is an acronym of "IntegratedDeveLopment Environment". It works well on both Unix and Windows platforms.It has a Python shell window, which gives you access to the Python interactive mode. It also hasa file editor that lets you create and edit existing Python source files.During the following discussion of IDLE's features, instead of passively reading along, youshould start IDLE and try to replicate the screenshots.Interactive Python shellWhen you start up IDLE, a window with an interactive Python shell will pop up:PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 10

Department of Computer Science & EngineeringYou can type Python code directly into this shell, at the ' ' prompt. Whenever you entera complete code fragment, it will be executed. For instance, typing: print "hello world"and pressing ENTER, will cause the following to be displayed:hello worldTry typing an underscore ( ). Can you see it? On some operating systems, the bottoms ofhanging letters such as 'g' or 'y', as well as underscores, cannot be seen in IDLE. If this is the casefor you, go to Options - Configure IDLE, and change the size of the default font to 9 or 11. Thiswill fix the problem!IDLE can also be used as a calculator: 4 48 8**3512Addition ( ), subtraction (-), multiplication (*), division (/), modulo (%) and power (**) operatorsare built into the Python language. This means you can use them right away. If you want to use asquare root in your calculation, you can either raise something to the power of 0.5 or you canimport the math moduleBelow are two examples of square root calculation: 16**0.54.0 import math math.sqrt(16)4.0The math module allows you to do a number of useful operations: math.log(16, 2)4.0 math.cos( 0 )1.0Note that you only need to execute the import command once after you start IDLE; however youwill need to execute it again if you restart the shell, as restarting resets everything back to how itwas when you opened IDLE.Creating scripts1.2.3.4.5.save your hello.py program in the /pythonpractice folder.Open up the terminal program. .Type cd /pythonpractice to change directory to your pythonpractice folder, and hitEnter.Type chmod a x hello.py to tell Linux that it is an executable program.Type ./hello.py to run your program!PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 11

Department of Computer Science & EngineeringProgram to add two integers.Take input from user.number1 input(" Please Enter the First Number: ")number2 input(" Please Enter the second number: ")# Using arithmetic Operator to add twonumbers sum float(number1) float(number2)print('The sum of {0} and {1} is {2}'.format(number1, number2, sum))Program to calculate area of a triangle:(a) Given Base and height of the triangle. Take input from user.(b) Given Three sides of a triangle (Make sure that it forms a triangle). Takeinput from user.# Python Program to find Area of a Trianglea float(input('Please Enter the First side of a Triangle: '))b float(input('Please Enter the Second side of a Triangle: '))c float(input('Please Enter the Third side of a Triangle: '))# calculate the PerimeterPerimeter a b c# calculate the semiperimeter s (a b c) / 2# calculate the areaArea (s*(s-a)*(s-b)*(s-c)) ** 0.5print("\n The Perimeter of Traiangle %.2f" %Perimeter);print(" The Semi Perimeter of Traiangle %.2f" %s); print("The Area of a Triangle is %0.2f" %Area)PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 12

Department of Computer Science & EngineeringPREFACEThis manual will introduce you to the Python programming language. It’s aimed at beginningprogrammers, but even if you’ve written programs before and just want to add Python to yourlist of languages, It will get you started.Python is a powerful high-level, object-oriented programming language created by Guido vanRossum. It has simple easy-to-use syntax, making it the perfect language for someone trying tolearn computer programming for the first time.This practical manual will be helpful for students of Computer Science & Engineering forunderstanding the course from the point of view of applied aspects.Though all the efforts have been made to make this manual error free, yet some errors mighthave crept in inadvertently. Suggestions from the readers for the improvement of the manualare most welcomedPYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 13

Department of Computer Science & EngineeringDO’S AND DONT’SDO’s1. Conform to the academic discipline of the department.2. Enter your credentials in the laboratory attendance register.3. Read and understand how to carry out an activity thoroughly before coming to thelaboratory.4. Ensure the uniqueness with respect to the methodology adopted for carrying out theexperiments.5. Shut down the machine once you are done using it.DONT’S1. Eatables are not allowed in the laboratory.2. Usage of mobile phones is strictly prohibited.3. Do not open the system unit casing.4. Do not remove anything from the computer laboratory without permission.5. Do not touch, connect or disconnect any plug or cable without your faculty/laboratorytechnician’s permission.PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 14

Department of Computer Science & EngineeringGENERAL SAFETY INSTRUCTIONS1. Know the location of the fire extinguisher and the first aid box and how to use themin case of an emergency.2. Report fire or accidents to your faculty /laboratory technician immediately.3. Report any broken plugs or exposed electrical wires to your faculty/laboratorytechnician immediately.4. Do not plug in external devices without scanning them for computer viruses.PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 15

Department of Computer Science & EngineeringDETAILS OF THE EXPERIMENTS CONDUCTED(TO BE USED BY THE STUDENTS IN THEIR RECORDS)DATE OFEXPT.TITLE OF THEPAGECONDUCTIONNoEXPERIMENTNo.S. 7891011PYTHON LANGUAGE PROGRAMMING LAB (RCS-454) ManualPage 16

PYTHON LANGUAGE PROGRAMMING LAB FILE (RCS 454)NameRoll No.Section- BatchData Structures Using C/ Java Lab (RCS-355) Manual (CS, III SEM)Page 16

Department of Computer Science & EngineeringINDEXExperimentExperimentDate ofDate N PROGRAMMING LAB MANUAL RCS-454, CS IVth SEMPage 17

Department of Computer Science & EngineeringEXPERIMENT 1OBJECTIVE:- Demonstrate the working of ‘id’ and ‘type’ functionsTHEORY:The id() function returns identity (unique integer) of an object.The syntax of id() is:id(object)As we can see the function accepts a single parameter and is used to return the identity of anobject. This identity has to be unique and constant for this object during the lifetime. Twoobjects with non-overlapping lifetimes may have the same id() value. If we relate this to C, thenthey are actually the memory address, here in Python it is the unique id. This function is generallyused internally in Python.Examples:The output is the identity of the object passed. This is random but when running in thesame program, it generates unique and same identity.Input : id(1025)Output : 140365829447504Output varies with different runsInput : id("geek")Output : 139793848214784# This program shows variousidentities str1 "geek"print(id(str1))str2 "geek"print(id(str2))# This will return Trueprint(id(str1) id(str2))# Use in Listslist1 ["aakash", "priya", "abdul"]print(id(list1[0]))print(id(list1[2]))# This returns falseprint(id(list1[0]) id(list1[2]))PYTHON PROGRAMMING LAB MANUAL RCS-454, CS IVth SEMPage 19

Department of Computer Science & EngineeringReturn Value from id():The id() function returns identity of the object. This is an integer which is unique for thegiven object and remains constant during its lifetime.Example:print('id of 5 ',id(5))a 5print('id of a ',id(a))b aprint('id of b ',id(b))c 5.0print('id of c ',id(c))The ‘type’ function:Python have a built-in method called as type which generally come in handy while figuring outthe type of variable used in the program in the runtime.The type function returns the datatype of any arbitrary object. The possible types are listed inthe types module. This is useful for helper functions that can handle several types of data.Example : Introducing type type(1) type 'int' li [] type(li) type 'list' import odbchelper type(odbchelper) type 'module' import types type(odbchelper) types.ModuleTypeTrue type takes anything -- and I mean anything -- and returns its data type. Integers, strings,lists, dictionaries, tuples, functions, classes, modules, even types are acceptable. type can take a variable and return its datatype. type also works on modules. You can use the constants in the types module to compare types of objects. This is what the infofunction does, as you'll see shortly.VIVA QUESTIONS:1.What data types are used in Python?2.What is the difference between functions used in python andPYTHON PROGRAMMING LAB MANUAL RCS-454, CS IVth SEMC? Are both the same?Page 20

Department of Computer Science & EngineeringEXPERIMENT -2OBJECTIVE:-To find all prime numbers within a given range.THEORY:Prime numbers: A prime number is a natural number greater than 1 and having no positivedivisor other than 1 and itself.For example: 3, 7, 11 etc are prime numbers.Composite number: Other natural numbers that are not prime numbers are called compositenumbers.For example: 4, 6, 9 etc. are composite numbers.Here is source code of the Python Program to check if a number is a prime number.r int(input("Enter upper limit: "))for a in range(2,r 1):k 0for i in range(2,a//2 1

This course is an introduction to the Python programming language for students without prior programming experience. We cover data types, control flow, object-oriented programming, and graphical user interface-driven applications. The examples and problems used in this course are drawn from diverse areas such as text processing, simple graphics creation and image manipulation, HTML and web .

Related Documents:

Python Programming for the Absolute Beginner Second Edition. CONTENTS CHAPTER 1 GETTING STARTED: THE GAME OVER PROGRAM 1 Examining the Game Over Program 2 Introducing Python 3 Python Is Easy to Use 3 Python Is Powerful 3 Python Is Object Oriented 4 Python Is a "Glue" Language 4 Python Runs Everywhere 4 Python Has a Strong Community 4 Python Is Free and Open Source 5 Setting Up Python on .

Python 2 versus Python 3 - the great debate Installing Python Setting up the Python interpreter About virtualenv Your first virtual environment Your friend, the console How you can run a Python program Running Python scripts Running the Python interactive shell Running Python as a service Running Python as a GUI application How is Python code .

Python is readable 5 Python is complete—"batteries included" 6 Python is cross-platform 6 Python is free 6 1.3 What Python doesn't do as well 7 Python is not the fastest language 7 Python doesn't have the most libraries 8 Python doesn't check variable types at compile time 8 1.4 Why learn Python 3? 8 1.5 Summary 9

site "Python 2.x is legacy, Python 3.x is the present and future of the language". In addition, "Python 3 eliminates many quirks that can unnecessarily trip up beginning programmers". However, note that Python 2 is currently still rather widely used. Python 2 and 3 are about 90% similar. Hence if you learn Python 3, you will likely

CR ASH COURSE PY THON CR ASH COURSE 2ND EDITION ERIC MATTHES SHELVE IN: PROGRAMMING LANGUAGES/ PYTHON 39.95 ( 53.95 CDN) LEARN PYTHON— FAST! COVERS PYTHON 3.X Python Crash Course is the world's best-selling guide to the Python programming language. This fast-paced, thorough introduction to programming with Python will

There are currently two versions of Python in use; Python 2 and Python 3. Python 3 is not backward compatible with Python 2. A lot of the imported modules were only available in Python 2 for quite some time, leading to a slow adoption of Python 3. However, this not really an issue anymore. Support for Python 2 will end in 2020.

Python Programming - This is a textbook in Python Programming with lots of Practical Examples and Exercises. You will learn the necessary foundation for basic programming with focus on Python. Python for Science and Engineering - This is a textbook in Python Programming with lots of Examples, Exercises, and Practical Applications

Archaeological excavation is the primary means in which we gather information. It is critical that it is carried out carefully and in a logical manner. The flow chart below has been provided to show the steps required for fully excavating and recording a feature. 3 Identify feature Clean area to find the extent of the feature Consider if pre-excavation photos and plan are required Select .