1 of 40

LOVELY PROFESSIONAL UNIVERSITY PUNJAB December, 2019

INT108:PYTHON PROGRAMMING

2 of 40

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

3 of 40

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

4 of 40

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

5 of 40

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

6 of 40

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.

7 of 40

Python on windows ?

Step 2: Now, click the “Install Now” link given in the above image. This starts the installation process of Python.

8 of 40

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.

9 of 40

Python on windows ?

After the progress is completed, you will see a setup successful message. The successful message screen is as given below.

10 of 40

Python on windows ?

Download latest version from Python.org , install it

>>>print(“Hello world”)

Hello world

11 of 40

Python on windows ?

Previous versions of python

3.10

3.9

3.8

3.7

2.7

Python is a interpreter based Language.

12 of 40

Python on windows ?

Example of Python Applications are

13 of 40

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.

14 of 40

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.

15 of 40

Memory management in Python

  • Memory in Python is managed by Python private heap space. All Python objects and data structures are located in a private heap. This private heap is taken care of by Python Interpreter itself, and a programmer doesn’t have access to this private heap.

  • Python memory manager takes care of the allocation of Python private heap space.

  • Memory for Python private heap space is made available by Python’s in-built garbage collector, which recycles and frees up all the unused memory.

16 of 40

Quiz on Python basics

Which one of the following is the correct extension of the Python file?

  1. .py
  2. .python
  3. .p
  4. None of these

Which character is used in Python to make a single line comment?

1. /

2. //

3. #

4. !

17 of 40

Questionnaire about Python basics

  1. What are the key features of Python?

  • What type of language is Python?

  • How is Python an interpreted language?

  • How is memory managed in Python?

  • How can we install Python on Windows ?

18 of 40

Variables , Expressions and Statements

  • A program is a set of instructions that specifies how to perform a computation.

  • Variable : is a name assigned to a value.

message=“hello”

n=13

pi=3.14159

>>>print message

hello

>>>print n

13

>>>print pi

3.14159

19 of 40

Variables , Expressions and Statements

  • Avoiding Name Error in case of variables

  • Rules : Variable name should begin with letter, though they can have both letters and numbers.

bruce and Bruce are two different variables.

  • Underscore character can be in name of variables

For example : Test_2_python_p

20 of 40

Variables , Expressions and Statements

  • Debugging

  • Programming errors are called bugs.

  • Process of tracking bugs and correct them is called debugging.

  • Types of Errors

  • Syntax Errors
  • Run Time Errors (Exceptions)
  • Semantic Errors

21 of 40

Keywords

  • Keywords define the language’s rules and structure and they can’t be used as variable names.

  • Python has twenty-nine keywords:
  • and def exec if not return
  • assert del finally import or try
  • break elif for in pass while
  • class else from is print yield
  • continue except global lambda raise

22 of 40

Input, Processing, and Output

  • The following example receives an input string from the user and saves it for further processing:

d_ft = int(input("Input distance in mile: "))

d_miles = d_ft * 5280.0

print("The distance in feet is %.2f feet." % d_miles)

23 of 40

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)

24 of 40

Input, Processing, and Output

25 of 40

Expressions

  • An expression is a combination of values, variables, and An expression is a combination of values, variables, and operators.

  • If you type an expression on the command line, the interpreter evaluates it and displays the result:

>>> 1 + 1

2

  • A value all by itself is considered an expression, and so is a variable.

>>> 17 >>> x

17 2

26 of 40

Statements

  • A statement is an instruction that the Python interpreter can execute.

  • When you type a statement on the command line, Python executes it and displays the result, if there is one.

  • A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

27 of 40

Behind the Scenes: How Python Works

28 of 40

Variables , Expressions and Statements

  • Syntax Errors : invalid code the compiler doesn't understand

  • printf(%d,s)

  • Run Time Errors (Exceptions)

  • a= 10/0;

  • Semantic Errors : valid code the compiler understands but it will do something else.

29 of 40

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

30 of 40

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

31 of 40

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.

32 of 40

Python statement

2. Write a Python statement that calculates and prints the number of seconds in 7 hours, 21 minutes and 37 seconds.

33 of 40

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.

34 of 40

Python statement

4. Python statement to calculate the square root of number

35 of 40

Test coding skills

  1. Program to enter two numbers and print the arithmetic operations like +,-,*, /, // and%.

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.

36 of 40

Questions ??

37 of 40

Mathematical terminologies for programming

  • The modulo operation(%) finds the remainder or signed remainder after the division of one number by another (called the modulus of the operation).

>>> 15 % 4

3

  • The power of some number in respect to some other number, that is called exponentiation (**). For Example : program 2 5 we do 2 ** 5 will give 32

  • A perfect number is a positive integer that is equal to the sum of its factors excluding the number itself. For example, 6 is a perfect number because when we add all its factors except 6, we get, 1 + 2 + 3 = 6.

38 of 40

Mathematical terminologies for programming

  • The Number is Armstrong

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.

39 of 40

Mathematical terminologies for programming

  • A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8.... The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms.

40 of 40

Thank You