Description
If you are learning Python from a beginner’s perspective and with hands-on approach then this is the right book. The major emphasis in this book has been to solve an exhaustive number of programming examples and to understand the concepts surrounding it right from the relevant theory, syntax, and semantics to coding issues. This book is useful for engineering students, beginners and those who want to learn a new programming language. The book adapts Python 3.1 version.
Table of Content
Chapter 1 Algorithmic Problem Solving
1.1 Algorithms
1.2 Definition of Algorithm
1.3 Characteristics of Algorithms
1.4 Building Blocks of Algorithms
1.4.1 Statements
1.4.2 State
1.4.3 Control Flow
1.4.4 Functions
1.5 Pseudo code
1.6 Flowcharts
1.7 Programming Language
1.8 Algorithmic Problem Solving
1.9 Simple Strategies for Developing Algorithms
1.9.1 Advantages of Iterative Algorithms
1.9.2 Advantages of Recursive Algorithms
1.10 Solved Examples
1.10.1 Write an Iterative Algorithm to Calculate and Print the Number
Of Digits in a Given Number
1.10.2 Write an Iterative Algorithm to Calculate and Print the Sum of
The Given Series
1.10.3 Write a Recursive Algorithm to Calculate the Greatest Common
Divisor of the Given Two Integer Numbers
1.11 Illustrative Problems
1.11.1 Write an Algorithm using Iterative Design to Find a Minimum
In a List
1.11.2 Write an Algorithm using Iterative Design to Insert a Card in a
List of Sorted Cards
1.11.3 Write an Algorithm using Iterative Design to Guess an Integer
Number in a Range
1.11.4 Write an Algorithm using Recursive Design to Solve the Towers
Of Hanoi Problem
1.12 Case Study
1.12.1 Write an Algorithm for Solving Linear Search using Recursive
Design
Chapter 2 Data Expressions and Statements
2.1 Python Interpreter and Inter active Mode
2.2 Values and Types
2.3 Variable
2.4 Python Data Types
2.5 Python Literals
2.5.1 Numeric Literals
2.5.2 String Literals
2.5.3 List of Literals in Python
2.5.4 Write a Python Program to Add Two Complex Numbers c1 = 2
+ 3j and c2 = 4 +7j
2.6 Python Constants
2.6.1 Write a Python Program using Boolean Data Types
2.7 Python Keywords
2.8 Expressions
2.8.1 Evaluate the Following Python Expression
2.8.2 Evaluate the Following Python Expression
2.9 Statements
2.10 Tuple Assignment
2.11 Operators in Python
2.11.1 Arithmetic Operators
2.11.2 Relational Operators
2.11.3 Assignment Operators
2.11.4 Python Bitwise Operators
2.11.5 Python Logical Operators
2.11.6 Python Identity Operators
2.11.7 Python Membership Operators
2.11.8 Type Conversion in Python
2.11.9 Type Promotion (Coercion) in Python
2.12 Comments
2.13 Modules and Functions
2.13.1 Syntax of User-defined Function
2.14 Illustrative Programs
2.14.1 Write a Python Program to Exchange the Value of Two Variables
2.14.2 Python Program to Circulate the Elements of a List
2.14.3 Python Program to Compute the Distance between Two Points
Chapter 3 Python Input and Output Statements
3.1 Input Statements in Python
3.1.1 input() function
3.2 Output Statements in Python
3.2.1 print() function
3.3 Python String Formatting Options
3.3.1 Write a program in Python to convert a given temperature in
Celsius to Fahrenheit and a given temperature in Fahrenheit to
Celsius
3.4 Python Math Library
3.5 Write a Python Program to Compute Simple Interest (SI) and Compound
Interest (CI) for a Given Principal, Rate of Interest and Duration in Years
3.6 Write a Python Program to Swap Two Variables without using a Third
Variable
3.7 Write a Python Program to Print the Ones Place of any Given Integer
Number
3.8 Write a Python Program to Compute the Area of a Triangle when the
Three Sides are given
Chapter 4 Control Flow, Functions
4.1 Conditionals: Boolean Values and Operators
4.2 Decision Making
4.2.1 If Statement
4.2.2 Alternative (If-Else)
4.2.3 Chained Conditional (If-elif-else)
4.2.4 Nested Conditional Statements
4.2.5 Dangling Else Problem
4.3 Iteration: State
4.3.1 While Loop
4.4 Infinite Loop
4.5 While Loop with Else
4.6 for Loop
4.6.1 Write a Python Program to Compute cos(x) from the Given Series
4.6.2 Write a Python program to Compute sin(x) from the Given Series
4.6.3 Write a Python Program to Compute Pythagorean Triples up to
A Given Maximum Value
4.7 for Loop with Else Statement
4.9 Write a Python Program to Build a Multiplication Table from 1 to 10
4.10 Python Break Statement
4.11 Role of Break Statement in While with Else Statement
4.12 Role of Break Statement in for with Else Statement
4.13 Python Continue Statement
4.14 Pass Statement in Python
4.15 Fruitful Functions
4.16 Return
4.17 Parameters
4.18 Default Parameter
4.19 Function Arguments in Python
4.19.1 Positional Arguments
4.19.2 Keyword Arguments
4.19.3 Variable-length Arguments
4.19.4 Call-by-value Versus Call-by-reference in Python
4.20 Local Variables and Global Variables
4.21 Function Composition in Python
4.22 Anonymous Function in Python
4.23 Write a Python Program using Functions to Generate the Floyd’s Triangle
for a Specified Number of Rows
4.24 Write a Python Program using Functions to Compute the Least Common
Multiple (LCM) of Two Given Integers
4.25 Recursive Function in Python
4.26 Strings
4.26.1 Strings in Python
4.26.2 Reading Strings from Keyboard
4.26.3 Accessing Strings
4.26.4 String Slices
4.26.5 String Immutability
4.27 String Functions and Methods
4.27.1 Built-in String Functions
4.27.2 Searching in a String
4.27.3 Splitting Strings and Removing Whitespaces
4.27.4 String Join, Length, Replace and Miscellaneous String Functions
4.28 String Module
4.28.1 String Module Constants
4.28.2 String Module Functions
4.29 Write a Python Program to Check Whether an Entered String is a
Palindrome or not without using Built-in String Functions Available in
Python
4.30 Write a Python Program to Count Separately the Vowels of a Given
String
4.31 Write a Python Program to Compute the Length of an Input String without using the Built-in Python Function “len()”
4.32 Write a Python Program to Count the Number of Characters in a Given
Word
4.33 Write a Python Program to Check the Bigger of the Two Inputted Strings
4.34 Write a Python Program to Count the Common Characters in the Two
Inputted Strings
4.35 Write a Python Program to Count the Occurrences of the Substring in a
Given String
4.36 Write a Python Program to Check Whether the Characters of an Inputted
String are in Alphabetical Order
4.37 Write a Python Program to Sort Words Entered by User in Alphabetical
Order
4.38 Write a Recursive Python Program to Find Permutation of All Characters
In a String
4.39 Lists as Arrays
4.39.1 How to Read a List Type from a Keyboard
4.40 Illustrative Programs
4.40.1 Python Program to Compute Square Root of a Given Number
4.40.2 Python Program to Compute the Greatest Common Divisor (GCD)
Of Two Integer Numbers
4.40.3 Python Program to Compute Exponentiation of Given Two
Numbers
4.40.4 Python Program to Compute the Sum of an Array of Numbers
4.40.5 Python Program to Implement Linear Search
4.40.6 Python Program to Implement Binary Search Algorithm
4.41 Case Study 1: Prime Number Generation
4.42 Case Study 2
4.43 Case Study 3
Chapter 5 Lists, Tuples, Dictionaries
5.1 Lists: List Operations
5.2 List Slices
5.3 List Methods
5.3.1 Built-in List Functions
5.3.2 Python List Methods
5.4 List Loop
5.5 List Mutability
5.6 Deleting Elements of a List
5.7 List Aliasing and Cloning Lists
5.8 Comparison of Two Lists
5.9 List as Parameters
5.10 Write a Python Program to Compute the Sum of Odd and Even Numbers
for a Given Range in a List
5.11 Write a Python Program to Compute Mean, Variance and Standard
Deviation of a List of Numbers
5.12 Write a Python Program to Compute Prime Numbers from 0 to Maximum
Range using Sieve of Eratosthenes
5.13 Write a Python Program to Compute the Average of Best of Two of
Three Assignment Tests. Maximum Marks for each Assignment is 25.
Fractional Marks in Final Average are Rounded off to the nearest and
Highest Whole Number
5.14 Write a Python Program to Reverse the Members of a Given List
5.15 Write a Python Program to Compute the Value of a Given Polynomial
of n Degree through Horner’s Method
5.16 Write a Python Program to Sort an Integer List using Bubble Sorting
Technique
5.17 Write a Python Program to Add Two given Matrices of Size m × n and
p ×q
5.18 Write a Python Program to Compute Multiplication of Matrices of Order
m × n and p ×q
5.19 Write a Python Program to Compute Transpose of a Given Matrix
5.20 Write a Python Program to Add and Find the Sum of Principal Diagonal
Elements of a Given Matrix
5.21 Write a Python Program to Add and Find the Sum of Secondary Diagonal
Elements of a Given Matrix
5.22 Write a Python Program to Compute the Norm of a Given Matrix
5.23 Write a Python Program to Compute Row Sum and Column Sum of a
Given Matrix
5.24 Write a Python Program to Compute Whether the Given Square Matrix
is a Magic Square
5.25 Tuples – Introduction
5.25.1 How to Read a List Type from a Keyboard
5.25.2 Accessing Elements of a Tuple
5.25.3 Modifying Elements of a Tuple
5.25.4 Deleting Elements of a Tuple
5.26 Basic Tuple Operations
5.27 Tuple Built-in Functions
5.28 Tuple Assignment
5.29 Tuple as Return Value
5.30 Write a Python Program using Tuples to Accept Individual Address
Details and Display the Same
5.31 Write a Python Program using Tuples to Accept Employee Salary Details
And Calculate Final Salary Based on the Overtime Hours
5.32 Write a Python Program using Tuples to Input Student Details. The
Program Should Accept a Given Student’s Roll Number and Display
His Specific Records
5.33 Write a Python Program using Tuples to Input Student Details and
Compute His Percentage and Class Based on His Average Marks for
Five Subjects
5.34 Write a Python Program using Tuples to Guess the Number of Colors
in a Traffic Signal and also to Guess which Color is used to say “Stop
if Possible!”. The Program Should Evaluate Whether the Answers are
Right or Wrong
5.35 Dictionaries: Operations
5.36 Accessing Dictionary Elements
5.37 Modifying a Dictionary
5.38 Delete Operations on a Dictionary
5.39 Dictionary Key Properties
5.40 Comparing Two Dictionaries
5.41 Dictionary Method
5.41.1 How to Generate Keys, Values and Items for Python Dictionary
5.41.2 Copying and Updating a Dictionary
5.42 Write a Python Program using Dictionary to Generate a Small Dictionary
Of Synonyms. The Program Should then Accept a Word and Generate
Synonyms for the Same
5.43 Write a Python Program using Dictionary to Accept a Sentence and
Generate the Frequency of Words for the Same
5.44 Write a Python Program to Create a Dictionary Consisting of State and
Their Corresponding Dishes. The Program Should Accept a State Name
From the user and Generate the Corresponding Dishes
5.45 Write a Python Program using Dictionary to Find the Maximum and
Minimum Among the Given Values
5.46 Write a Python Program using Dictionary to Encrypt and Decrypt a
Given Plain Text using Additive Cipher
5.47 List Comprehension
5.48 Illustrative Programs
5.48.1 Python Program to Implement Selection Sort
5.48.2 Python Program to Implement Insertion Sort
5.48.3 Python Program to Implement Merge Sort
5.48.4 Python Program to Draw a Histogram using Turtle Graphics
5.49 Case Study
Chapter 6 Files, Modules, Packages
6.1 Introduction
6.2 Text Files
6.3 Reading from Files
6.4 Reading Lines from Files
6.5 Stripping Characters from Files
6.6 Writing into Files
6.7 Filenames and Paths
6.8 Format Operator
6.9 Command Line Arguments
6.9.1 The System Module—sys Module
6.9.2 Write a Python Program that takes Command Line Arguments
as Integer Numbers and Computes their Sum
6.10 Exceptions in Python
6.11 Write a Python Program to Demonstrate IO Error Exception when we try
to Read a Non-existing File
6.12 Write a Python Program that Computes the Sum of All Integer Numbers
Stored in a File Named “data.txt” with an Exception Handling Feature
if the Numbers are not Integers. The Sum should be Printed on the
Command Prompt
6.13 Write a Python Program to Count the Number of Lines in a File
6.14 Write a Python Program to Find the Most Frequent Words in a Text Read
From a File
6.15 Write a Python Program that Reads an Input File Containing Floating
Point Values and Find the Minimum and the Maximum of the Values
Stored
6.16 Write a Python Program to Compute the Number of Lines, Words and
Characters in a Given Input Test File
6.17 Python Modules
6.17.1 Importing Modules
6.17.2 How to Create Your Own Modules
6.17.3 To Invoke Specific Functions or Variables from Modules
6.17.4 The dir () Function
6.17.5 Restoring a Module
6.18 Python Packages
6.19 Illustrative Programs
6.19.1 Python Program to Perform Word Count on a given Text File
6.19.2 Python Program to Copy a File
Section II – GE8161
Part A Laboratory
1. GCD of two numbers
2. Square root of a number (Newton’s)
3. Exponentiation (power of a number)
4. Maximum of a list of numbers
5. Linear search
6. Binary search
7. Selection sort
8. Insertion sort
Contents xvii
9. Merge sort
10. First n prime numbers
11. Multiply matrices
12. Command line arguments (word count)
13. Most frequent words in a text read from a file
14. Elliptical orbits in Pygame
15. Bouncing ball using Pygame
Part B Solved Examination Laboratory
1. Code a Python program to accept a number, find the sum of digits of the
number and print the result
2. Code a Python program to accept two numbers m and n where m > n,
find the quotient, remainder and print the result
3. Code a Python program that accepts a character string and a character
as argument, and delete all the occurrences of this character in the string
and display the result
4. Code a Python program to find the sum of first ‘n’ odd numbers and print
the result
5. Code a Python program to merge two lists
6. Code a Python program to find the 1st, 2nd and 3rd highest element in a
list
7. Code a python program that reads positive integer and then prints out all
the positive divisors of that integer
8. Code a Python program that will accept a roll number, name and total
marks obtained by a student and assign grades according to the following
conditions, and display the roll number, name, total marks and grade
9. Code a Python program that will accept a one character grade code of an
employee and depending on what grade code is input, display the basic
pay of the employee according to the table given below
10. Code a Python program that reads a line of text on a character-by-character
basis, and then displays the characters in reverse order. Your program
must use recursion to carry out the reversal of characters
11. Code a Python program that reads a character and prints out whether or
not it is a vowel or a consonant
12. Code a Python program to read the user id, name and the number of
units consumed and compute the electricity charges
13. Code a Python program to print the first ‘n’ numbers divisible by 7
14. Code a Python program to compute the sum of the series
15. Write aPython program to swap two numbers using functions
16. Write a Python program to find whether the given number is positive or
negative
17. Write a python program to find whether a given number is even or odd
18. Write a python program to find the sum of ‘n’ natural numbers using for
loop
19. Write a Python program to − calculate the length of a string, reverse
words in a string, display the same string multiple times, concatenate
two strings, slice a string
20. Write a Python program to − add new elements at the end of the list,
reverse elements in the list, display same list elements multiple times,
concatenate two lists, sort the elements of the list in ascending order
21. Write a Python program for cloning the list
22. Write a Python program for aliasing a list
23. Write a Python program: “tuple1 = (10, 50, 20, 40, 30)” − to display
the elements 10 and 50 from tuple1, to display the length of tuple1, to
find the minimum element from tuple1, to add all elements in tuple1, to
display the same tuple1 multiple times
24. Write a Python program to − create a dictionary, add an element to a
dictionary, display the length of the dictionary, update the element in a
dictionary, remove all the elements from the dictionary
Part C Additional Solved Laboratory
1. Write a Python program to compute the area of trapezium
2. Write a user-defined function in Python to check whether the given number
Is prime or not
3. Write a Python program to compute to calculate and print the sum of the
Following series
4. Write a Python program to compute Gregory series for calculating the
value of π
5. Write a Python program to compute the sum and average of numbers
from0upton
6. Write a Python program to generate the Tribonacci sequence for n
numbers
7. Write a Python program to generate the perfect numbers up to a
max range
8. Write a Python program to compute Highest Common Factor (HCF) of
Two numbers
9. Write a Python program to generate Lucas sequence of N numbers
10. Write a user-defined Python function permute (x, y) which returns the
number of different permutations of y items taken from a set of x items
11. Write a user-defined Python function combination (x, y) which returns
the number of different unordered combinations of y items taken from a
set of x items
12. Write a Python program to remove duplicate words from a given string
13. Write a Python program to capitalize each word in a given sentence (First
letter of each word should be capital)
14. Write a Python program to return words beginning with specified letter
in a sentence also display the count
15. Write a Python program to insert white spaces before and after the string
16. Write a Python program to remove extra white spaces between two words
In a string
17. Write a Python program to return quoted word from a sentence
18. Write a Python program to accept password as a string. If the length of
password is less than 6 characters display a message as “weak password”.
If length of password is between 6 and 10 characters then display a
message as “strong password” and if the length of password is more
than 10 characters and up to 15 characters then display a message as
“very strong password”
19. Write a Python program to accept password as a string. The password
String must consist of uppercase alphabets, lowercase alphabets, numbers,
and special characters. The program should check the presence of at
least one uppercase alphabet, lowercase alphabet, at least one number,
and at least one special character. And return the message as “Password
is acceptable” or “Password is not acceptable”
20. Write a Python program to accept a list of numbers and a number. The
program should print in order all elements of the list that are greater than
the number
21. Write a Python program to accept a list of numbers and sort half of them
in ascending and remaining half in descending
22. Write a Python program to convert all the principal diagonal elements
to1’s
23. Write a Python program to compare two matrices of same size and print
(“They are same”) if all the elements are equal else print (“They are
unequal matrices”)
24. Write a Python program to check if a given matrix is a lower triangular
matrix
25. Write a Python program to check if a given matrix is a upper triangular
matrix
26. Write a Python program to check whether a given matrix is a symmetric
matrix
27. Write a Python program using tuples to compute if the number of odd
value elements and even value elements are the same
28. Write a Python program to search a name in a given tuple of names and
if the name is found return the name along with its position
29. Write a Python program to simulate the word jumble game. The input
is the scrambled word and the player has to guess the same in maximum
10attempts or else he loses
30. Write a Python program that reads a dictionary and counts the number
of values that appears two or more times
31. Write a Python program which acts as an English to Tamil translator.
The program should accept an English word from the user and return
the corresponding Tamil word
32. Write a Python program to add integer numbers stored in two files and
their sum should be written in another file
Section III
Appendix A Pygame Installation
A.1 Introduction
A.2 InstallationofPython3.2
A.3 Pygame Installation
A.4 To Test Pygame is Installed Properly or Not
A.5 Introduction to Pygame
A.6 Python program: HelloWorld.py
A.7 Python Program: Circle.py
Appendix B Mini Projects Using Python
B.1 Cows and bulls game
B.2 Number guessing game
B.3 Hollywood hangman game
B.4 Encrypt and decrypt a message using Vigenere Cipher
Appendix C Viva Voce Questions
Appendix D Solved University Question Papers
Bibliography
About The Author
Dr. S. A. Kulkarni is Associate Professor, Department of Computer Science and Engineering, The National Institute of Engineering (NIE), Mysuru. He has a Ph.D. in the faculty of Computer and Information Science from The National Institute of Engineering Research Center, Mysuru, affiliated to Visvesvaraya Technological University, Karnataka.
New Product Tab
Here's your new product tab.
Reviews
There are no reviews yet.