LOVELY PROFESSIONAL UNIVERSITY PUNJAB December, 2019
INT108:PYTHON PROGRAMMING
Course outcome
CO1 :: define the installation of python environment and basics of Python language
CO2 :: apply the condition and iteration statements for evaluating the appropriate alternates
CO3 :: apply to formulate Regular Expressions and use them for Pattern Matching
Course outcome
CO4 :: construct the core data structures like lists, dictionaries, tuples and sets in Python to store, process and sort the data
CO5 :: Apply the concepts of Object-oriented programming as used in Python using encapsulation, polymorphism, and inheritance
CO6 :: Apply the external modules for creating and writing data to excel files and inspect the file operations to navigate the file systems
Setting up Your Programming Environment
# Python versions, Python on windows,
running a ‘Hello World’ program
#Variables, Expression and Statements: :
Naming and using variables,
Avoiding Name Error when using variables, Values and types,
Variables name and keywords, statements,
Operators and operands,
Order of operations,
Operations on string,
Composition and comments
What is Python ?
Python is an open source, object-oriented, high-level powerful programming language.
Developed by Guido van Rossum in the early 1989. Named after Monty Python
Python runs on many Unix variants, on the Mac, and on Windows 2000 and later.
Latest version available is 3.10.6
Python is Case sensitive Language
Python on windows ?
Step 1: Download the Python Installer binaries. Open the official Python website in your web browser. Navigate to the Downloads tab for Windows.
Python on windows ?
Step 2: Now, click the “Install Now” link given in the above image. This starts the installation process of Python.
Python on windows ?
You can check the installation progress as given in the screen above. This installs all the libraries of Python on to your local system.
Python on windows ?
After the progress is completed, you will see a setup successful message. The successful message screen is as given below.
Python on windows ?
Download latest version from Python.org , install it
>>>print(“Hello world”)
Hello world
Python on windows ?
Previous versions of python
3.10
3.9
3.8
3.7
2.7
Python is a interpreter based Language.
Python on windows ?
Example of Python Applications are
Salient Features of Python
Simple- Python is a simple language. Reading a good Python program feels almost like reading English. It allows you to concentrate on the solution to the problem rather than the syntax i.e. the language itself.
Easy to Learn-Python is extremely easy to get started with. Python has an extraordinarily simple syntax.
Open source : Python is publicly available open source software, any one can use source code that doesn't cost anything.
Salient Features of Python
Portable :High level languages are portable, which means they are able to run across all major hardware and software platforms with few or no change in source code. Python is portable and can be used on Linux, Windows, Macintosh and many more.
Object-Oriented : Python is a full-featured object-oriented
programming language, with features such as classes, inheritance, objects and overloading.
Python is Interactive : Python has an interactive console where you get a Python prompt(command line) and interact with the interpreter directly to write and test your programs. This is useful for mathematical programming.
Memory management in Python
Quiz on Python basics
Which one of the following is the correct extension of the Python file?
Which character is used in Python to make a single line comment?
1. /
2. //
3. #
4. !
Questionnaire about Python basics
Variables , Expressions and Statements
message=“hello”
n=13
pi=3.14159
>>>print message
hello
>>>print n
13
>>>print pi
3.14159
Variables , Expressions and Statements
bruce and Bruce are two different variables.
For example : Test_2_python_p
Variables , Expressions and Statements
Keywords
Input, Processing, and Output
d_ft = int(input("Input distance in mile: "))
d_miles = d_ft * 5280.0
print("The distance in feet is %.2f feet." % d_miles)
Input, Processing, and Output
The input function always builds a string from the user’s keystrokes and returns it to the program Strings that represent numbers must be converted from strings to appropriate number types
Two type conversion functions: int (for integers) and float (for floating-point numbers)
Input, Processing, and Output
Expressions
>>> 1 + 1
2
>>> 17 >>> x
17 2
Statements
Behind the Scenes: How Python Works
Variables , Expressions and Statements
Detecting and Correcting Syntax Errors (1 of 2)
Programmers inevitably make typographical errors when editing programs, called syntax errors
When Python encounters a syntax error in a program, it halts execution with an error message
Example:
>>> length = int(input(“Enter the length: ”))
Enter the length: 44
>>> print(lenth)
Traceback (most recent call last):
File “<py shell#l>”, line 1, in <module>
NameError: name ‘lenth’ is not defined
Detecting and Correcting Syntax Errors (1 of 2)
The next statement attempts to print the value of the correctly spelled variable:
>>> print(length)
SyntaxError: unexpected indent
Final example, programmer attempts to add two numbers, but forgets to include the second one:
>>> 3 +
SyntaxError: invalid syntax
Python statement
1. There are 10mm in a 1 cm. Write a Python statement that calculates and prints the number of mm in 987 cm.
Python statement
2. Write a Python statement that calculates and prints the number of seconds in 7 hours, 21 minutes and 37 seconds.
Python statement
3. The perimeter of a rectangle is 2w+2h, where w and h are the lengths of its sides. Write a Python statement that calculates and prints the length in inches of the perimeter of a rectangle with sides of length (width)4 and (height)7 inches.
Python statement
4. Python statement to calculate the square root of number
Test coding skills
2. Write a program to find whether an inputted number is perfect or not.
3. Write a Program to check if the entered number is Armstrong or not.
4. Write a Program to find factorial of the entered number.
5. Write a Program to enter the number of terms and to print the Fibonacci Series.
Questions ??
Mathematical terminologies for programming
>>> 15 % 4
3
Mathematical terminologies for programming
The factorial of a whole number is the function that multiplies the number by every natural number below it.
The meaning of 5 factorial is that we need to multiply the numbers from 1 to 5.
That means, 5! = 5 × 4 × 3 × 2 × 1 = 120.
Mathematical terminologies for programming
Thank You