Professional Documents
Culture Documents
Module 1
Module 1
Computer
B.Tech. Science and
3rd Semester
Engineering
Module 2: Python Data Structures 6Hrs
Python Data Structures: Strings, Arrays, Lists, Tuples, Sets and Dictionaries
Module 5: PYTHON FILES & LIBRARIES 5Hrs
Files: File types, modes, File functions, File attributes, File positions, Looping over file.
Basics of NumPy and Pandas
Text Books:
► 1. “Python for Everybody-Exploring Data Using Python 3”, Dr. Charles R. Severance, 4
Shroff Publishers; First edition (10 October 2017)
Reference Books:
► 1. “Computer Science Using Python: A Computational Problem- Solving Focus”,
Charles Dierbach, Introduction John Wiley, 2012.
► 2. “Introduction to Computation and Programming Using Python”, John V Guttag,
Prentice Hall of India, 2015.
► 3."How to think like a Computer Scientist, Learning with Python", Allen Downey,
Jeffrey Elkner and Chris Meyers, Green Tea Press, 2014.
► 4. “Learning to Program with Python”, Richard L. Halterman, 2011.
Mark Distribution – Integrated Courses (Theory 5
+ Lab)
CIA SEE
(60 Marks) (40 Marks)
► Evaluate an algorithm and analyse the how its performance is affected by the size of the input
so that we can choose the best algorithm for problem.
What is Python? 14
Algorithm
Draw a flowchart
► Issues:
► If the list is longer then, it will take long time for searching. how to solve it?
30
Algorithmic complexity
+ Addition
That variable can then be printed just like any other variable.
Example
Output
Activity
Write a program to display the result of a student. You should get the name of the student,
marks obtained in programming, English, and Mathematics from the user. Finally, display
the result. The sample output is attached below.
Example
Your name : Sonam Dorji
Marks in Dzongkha:98
Marks in English :88
Marks in Mathematics:100
--------------------------
-
Name : Dorji Dema
Dzongkha: 98
English: 88
Mathematics: 100
Type Conversion
• When we use the input() function, python interprets everything the user enters as a string.
• But we can also accept the input type based on program requirements. If we are writing a
program to do the simple mathematical calculation, our program should accept only numbers,
i.E. Either integer or float.
• Similarly, if we are writing a program to collect the name of the students, our program should
accept input as a string type.
Type Conversion
• WE CAN ACHIEVE THIS BY CONVERTING THE TYPE OF DATA USING A
BUILT-IN DATA TYPE FUNCTIONS SUCH AS INT(), FLOAT(), AND STRING(),
AND IT SHOULD BE PLACED BEFORE THE INPUT() FUNCTION.
Converts user input into integer data type
Example
age = int(input("Enter your age:"))
print(f"Your age is {age}")
Output
Enter your age:30
Your age is 30
Type Conversion
• SIMILARLY, WE CAN DO THE SAME WITH OTHER DATA TYPES USING
ITS BUILT-IN FUNCTIONS.
Example
height= float(input("Enter your height:"))
print(f"Your height is {height}")
Output
Enter your height:30
Your height is 30.0
Checking the type of data
TO CHECK THE TYPE OF A DATA, USE BUILT-IN FUNCTION TYPE().
Example
Output String
What is your name: Dawa
Your name is Dawa And it's data type is <class 'str'>
Activity
Write a program that accepts data from a user. Your program should accept your name, age,
and height. A name should be a string, age should be an integer and the height should be
float.
Example
Activity
Write a program that accepts your name and five subjects mark.
Print an output where you will see your name and five subjects
mark including your average mark.
67
Comments in Python
• Why comment?
# All done
print bigword, bigcount
69
Multi – Line Comments in Python
'''This is a
multi-line
comment
'''
Exercise
70
Main.py
Output
lang = “ Python" Welcome to Python Programming
print("Welcome to", lang, "Programming") Printing lang three times:
print("Printing lang three times:", lang*3) Python Python Python
1. Manual string formatting
Cont.
Main.py
print("ENG", "MATH", "DZO", "GEO", sep="|", end="***")
Output
ENG|MATH|DZO|GEO***
2. Formatted string literals or f-strings
Formatted string literals (also called f-strings for short) let you include the value
of Python expressions inside a string by prefixing the string with f or F and writing
expressions as {expression}.
It works only in python version 3.6 and above.
Example:
Output
Main.py
Dawa you are 23 years old
name = "Dawa“
age = 23
print(f"{name} you are {age} years old")
3. The string format() method
Output
We can also pass a number inside a bracket. A number in the brackets can
be used to refer to the position of the object passed into the format()
method.
Main.py
Output
5 3 7 4
Cont. 3. The string format() method
Main.py
PI = 3.14159265
print('The value of PI is {:.2f}'.format(PI))
Output
The value of PI is 3.14
4. Old string formatting or printf() style in C
String objects have one unique built-in operation: the % operator (modulo). This
is also known as the string formatting or interpolation operator. Given format %
values (where the format is a string), % conversion specifications in format are
replaced with zero or more elements of values.
Example:
Main.py
print('%d %s cost Nu.%.2f' % (6, 'bananas', 60))
Output
6 bananas cost Nu.60.00
1. Write a maximum of 5 lines of the python program describing yourself. Your program should
have three variables declared (Ex. name, age, and height).
The expected output:
Conditional Statement 82
1. The conditional statements in programming languages decide the direction of the flow
Syntax:
if condition:
Statement
***else statement will not take any condition. Else
else:
statement will be executed only if the if statement
Statement
condition fails.
You should know comparison operators to write condition 85
Your program should get the age of the voter from the user and if their age is 18 and above let them vote
otherwise deny them from voting.
elif statement 88
► The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation and used when there
► If the condition is not true, it will check for elif condition. Otherwise, it will directly execute the else
statement.
Syntax:
if condition 1:
Statement
elif condition 2:
Statement
elif condition 3:
Statement
elif condition can continue………
Example 89
The if and elif statement can execute multiple conditions at the same time. Multiple conditions can be
if condition 1:
Statements
if condition 1.1:
Statement
else:
Statement
else:
Statement
Example 96
age = int(input("Enter your age:"))
► The pass statement does nothing. It can be used when a statement is required syntactically correct but
► The pass can be also used as a placeholder for a function or conditional body when you are working on a
new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored.
Example
age = 13 Output:
if age <= 12:
In the above example, nothing will be printed and it
pass won’t generate any error.
Loops in python programming 99
Syntax:
For iterator_variable in sequence:
statement(s)
102
Example
Continue Pass
Break
for i in range(5): for i in range(10):
for i in range(10):
if i == 3: Pass
if i == 3:
continue
break
print(i)
print(i)
Output:
0
Output:
1
0
1 2 Output:
2 4 Nothing will be printed
Activity 108
➔ while loop repeatedly carries out a target statement while the condition is true. The loop
Output:
Hello!
I like Python.
Activity 112
def max(inp):
blah
“Hello world” blah ‘w’
(a string) for x in y: (a string)
blah
blah
def print_lyrics():
print "I'm a lumberjack, and I'm okay.”
print 'I sleep all night and I work all day.'
print "I'm a lumberjack, and I'm 129
okay."
print_lyrics():
x=5 print 'I sleep all night and I work all day.'
print 'Hello'
print 'Yo'
print_lyrics() Hello
x=x+2 Yo
print x I'm a lumberjack, and I'm okay.I
sleep all night and I work all day.
7
Arguments 132
def greet():
return "Hello” Hello Glenn
Hello Sally
print greet(), "Glenn”
print greet(), "Sally"
Return Value >>> def greet(lang): 135
... if lang == 'es':
... return 'Hola’
475 = 40 * 10 + 5 * 15