Python Programming Language I year B Sc Computer

Python Programming Language I year B. Sc. Computer Science - Shift-I 2020 -21 batch

Short History of Python Ø The Python programming language was developed in the late 1980 s by Dutch programmer Guido van Rossum while working at CWI (the Centrum voor Wiskunde en Informatica in Amsterdam, Netherlands). Ø The language was not named after the large snake species but rather after the BBC comedy series Monty Python’s Flying Circus. Guido van Rossum happens to be a fan. Ø Just like the Linux OS, Python eventually became an open source software development project. However, Guido van Rossum still has a central role in deciding how the language is going to evolve. Ø Python is a general-purpose language that was specifically designed to make programs very readable. Ø Python also has a rich library making it possible to build sophisticated applications using relatively simple-looking code.

Setting Up the Python Development Environment • versions of Python in use. • Python 3 series is a new version of Python that fixes some less-than-ideal design decisions made in the early development of the Python language. • IDE installation using the standard Python development kit that includes the IDLE IDE. You may download the kit (for free) from: • http: //python. org/download/

Python interactive shell window Ø To get started with Python, you need to open a Python interactive shell window. The IDLE interactive shell included with the Python IDE is shown in Figure.

Python IDLE • At the >>> prompt, you can type single Python instructions. • The instruction is executed by the Python interpreter when the Enter/Return key is pressed. • The interactive shell expects the user to type a Python instruction.

Python IDLE • When the user types the instruction print('Hello world') and then presses the Enter/Return key on the keyboard, a greeting is printed like this: >>> print('Hello world') Hello world

Computational Thinking Ø Computational thinking is a term used to describe the intellectual approach through which natural or artificial processes or tasks are understood and described as computational processes. Ø To model the relevant aspects of the task and describe the task as an algorithm, we must understand the task from a “computational” perspective.

A Sample Problem Ø We are interested in purchasing about a dozen prizewinning novels from our favourite online shopping web site. Ø The thing is, we do not want to pay full price for the books. We would rather wait and buy the books on sale. More precisely, we have a target price for each book and will buy a book only when its sale price is below the target. Ø So, every couple of days, we visit the product web page of every book on our list and, for each book, check whether the price has been reduced to below our target.

Algorithm 1. Let N be the number of products in list Addresses. For every product I = 0, 1, . . . , N-1, execute these statements: 2. Let ADDR be the address in list Addresses for product I. 3. Download the web page whose address is ADDR and let PAGE be the content of this web page 4. Find in PAGE the current price of product I and let CURR be this value 5. Let TARG be the product I target price from list Targets If CURR < TARG: Print ADDR

Algorithm for Computational thinking a. We compare numbers CURR and TARG b. We find the address of product I in list Addresses c. We search the web page content for a price d. We create a sequence 0, 1, 2, . . . , N-1 from integer N

• IDLE STANDS FOR INTEGRATED DEVELOPMENT LEARNING ENVIRONMENT • WHAT IS PYTHON SHELL? • PYTHON SHELL IS USED TO EXECUTE A SINGLE PYTHON COMMAND GIVES RESULT. • IT IS ALSO CALLED AS PYTHON INTERACTIVE SHELL OR PYTHON INTERPRETER.

Example: A = 10 B=5 Find out the greatest among A and B If(A>B) then print A is greater else Print B is greater

Computational thinking is applying logic on inputs and do the process to obtain the required output.

What is mean by algorithm? Algorithm is a step by step approach to write any program. What is Flow chart? Flow chart is a diagrammatical representation of an algorithm

Computational thinking : Biggest among two numbers- Algorithm • • • Start the program Get the input of A and B ; A = 10, B=5 Compare A and B ; if(A>B) Print A is greater Print B is greater Stop the program.

Computational thinking : Biggest among two numbers- Flow chart

A=25 B=20 C=10 FIBONACCI SERIES 0, 1, 1, 2, 3, 5

Python Data Types • Data types in Python • Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables are instance (object) of these classes. • There are various data types in Python. Some of the important types are listed below.

Data Types: Numbers Python supports integers, floatingpoint numbers and complex numbers. They are defined as int , float , and complex classes in Python. Integers and floating points are separated by the presence or absence of a decimal point. For instance, 5 is an integer whereas 5. 0 is a floatingpoint number.

Example for Numbers Ex: 1) >>> 2 + 2 Output: 4 Ex: 2 >>> 50 - 5*6 Output: 20 Ex: 3 >>> (50 - 5*6) / 4 Output: 5. 0 Ex 4: >>> 8 / 5 # division always returns a floating point number Output: 1. 6

Example for Numbers Ex: 5 >>> 17 / 3 # classic division returns a float Output: 5. 66666667 Ex: 6 >>> 17 // 3 # floor division discards the fractional part Output: 5 Ex: 7 >>> 17 % 3 # the % operator returns the remainder of the division Output: 2 Ex: 8 >>> 5 * 3 + 2 # result * divisor + remainder Output: 17

Example for Numbers Ex: 9 >>> 5 ** 2 # 5 squared Output: 25 Ex: 10 >>> 2 ** 7 # 2 to the power of 7 Output: 128 Ex: 11 >>> width = 20 >>> height = 5 * 9 >>> width * height Output: 900

Expressions, Variables, and Assignments Algebraic Expressions and Functions: At the interactive shell prompt >>> , we type an algebraic expression, such as 3 + 7, and hit the Enter key on the keyboard to view the result of evaluating the expression: >>> 3 + 7 10 Expressions that use different algebraic operators: >>> 3 * 2 output: >>> 5 / 2 Output: 2. 5 >>> 4 / 2 Output: 2. 0 6 Expression is defined as the combination of operands and operators Ex: a+b - c/d

Expressions Algebraic Expressions and Functions: In the first two expressions, integers are added or multiplied and the result is an integer. In the third expression, an integer is divided by another and the result is shown in decimal point notation. The rule in Python is to return a number with a decimal point and a fractional part, even when the result is an integer. This is illustrated in the last expression, where integer 4 is divided by 2 and the result shown is 2. 0 rather than 2.

Other algebraic operators Exponentiation operator **: Ex: >>> 2**3 Output: 8 Ex: >>> 2**4 Output: 16

// and % operators in Number data type • The // operator in expression a//b returns the integer quotient obtained when integer a is divided by integer b. • The % operator in expression a%b computes the remainder obtained when integer a is divided by integer b. • For example: >>> 14 // 3 Output: 4 • For example: >>> 14 % 3 Output: 2

Mathematical Functions in Python also supports mathematical functions that can be used in an algebra. In algebra, the notation f(x) = x + 1 is used to define function f() that takes an input, denoted by x, and returns a value, which is x+1. For Example: x=3 F(3)=3+1 Output: 4

Absolute Function • For example, the Python function abs() can be used to compute the absolute value of a number value: Example: >>> abs(-4) Output: 4 Example: >>> abs(-3. 2) Output: 3. 2

Playstore Google Classroom Download and install

min() and max() functions • Some other functions in Python are min() and max(), which return the minimum or maximum, respectively, of the input values: Example: >>> min(6, -2) Output: -2 Example: >>> max(6, -2) Output: 6 Example: >>> min(2, -4, 6, -2) Output: -4 Example: >>> max(12, 26. 5, 3. 5) Output: 26. 5

Do the following in Python installed in your mobile or laptop and note the answers for each 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 4. 2+3. 5 -15. 5 + -23 112+136 X=10, y=15, z=x+y Z=x*y Z=x/7 Z=x%y Z=x//y X=5<10 Y=-5>-10 a=10, b=10, a==b min(2, 3, 1, 0, 10) max(2, 3, 1, 0, 10) min(-2, 0, -5, 1) max (-2, 0, -5, 1) abs(-51. 0) abs(10. 0) abs(-10. 0) x = 5/2. 0+3* 5. 0 x= 10, y=15, z=(y<15) a=-5, b=5, z=(a==b) print(‘your name’) Print(‘ Enter input’) z = a+b-c/d (give values for a, b, c&d) z=a-b/c*d (give values for a, b, c&d)

Strings • The Python string type, denoted by str, is used to represent and manipulate text data or, in other words, a sequence of characters, including blanks, punctuation, and various symbols. A string value is represented as a sequence of characters that is enclosed within quotes: • Example: • >>> ‘Hello world’ • >>> s=‘hello’

Strings operators • Python provides operators to process text (i. e. , string values). • Like numbers, strings can be compared using comparison operators: ==, !=, < , >, and so on. • Operator ==, for example, returns True if the strings on either side of the operator have the same value:

Strings operators- Comparison Example >>> s=‘hello’ >>>s hello >>>s =='hello' True >>> t ='world‘ >>>t world >>> s != t True >>> s == t False

Strings comparison- Example • While == and != test whether two strings are equal or not, the comparison operators < and > compare strings using the dictionary order: >>> s < t True >>> s > t False

Strings operators-Concatenation • The + operator, when applied to two strings, evaluates to a new string that is the concatenation (i. e. , the joining) of the two strings: >>> s + t 'helloworld' >>> s +''+t 'hello world'

Strings operators-Multiplications by an integer • When a string gets multiplied by an integer, then >>> 3 *'A' 'AAA' >>> 'hello ' *2 'hello ' >>> 30 * '-' '---------------' • Multiplying a string s by an integer k gives us a string obtained by concatenating k copies of string s. Multiplying string '-' 30 times.

Strings operators- in operator • With the in operator, we can check whether a character appears in a string: >>> s ='hello' >>> 'h' in s True >>> 'g' in s False

Strings operators- in operator

Strings - Excercises 1. a= ‘america’ b=‘antartica’ Find out a==b, a<b, a>b and a!=b Concatenate a and b 2. Do the following: a= ‘california’ b= ‘canada’ Find cal in a, Find for in a, find d in a, find c in a 3. Multiply the following: ‘BBB’*10, ‘IAM’*3, “@”*20

Indexing Operator • The individual characters of a string can be accessed using the indexing operator []. • We define the concept of an index first. • The index of a character in a string is the character’s offset (i. e. , position in the string) with respect to the first character. The first character has index 0, the second has index 1 (because it is one away from the first character), the third character has index 2, and so on. • • • The indexing operator [] takes a non-negative index i and returns a string consisting of the single character at index i. >>>s=‘hello’ >>> s[0] 'h' >>> s[1] 'e' >>> s[4] 'o'

Indexing Operator • The individual characters of a string can be accessed using the indexing operator []. • We define the concept of an index first. • The index of a character in a string is the character’s offset (i. e. , position in the string) with respect to the first character. The first character has index 0, the second has index 1 (because it is one away from the first character), the third character has index 2, and so on. • • • The indexing operator [] takes a non-negative index i and returns a string consisting of the single character at index i. >>>s=‘hello’ >>> s[0] 'h' >>> s[1] 'e' >>> s[4] 'o'

Indexing Operator • Negative indexes may be used to access the characters from the back (right side) of the string. For example, the last character and second to last can be retrieved using negative indexes -1 and -2, respectively. • >>> s[-1] • 'o' • >>> s[-2] • 'l'

Lists • In Python, lists are usually stored in a type of object called a list. • • A list is a sequence of objects. The objects can be of any type: numbers, strings, even other lists. For example, we assign the variable pets as the list of strings representing several pets: • >>> pets = ['goldfish', 'cat', 'dog'] • • • The variable pets evaluates to the list: >>> pets ['goldfish', 'cat', 'dog'] • In Python, a list is represented as a comma-separated sequence of objects enclosed within square brackets. An empty list is represented as []. • Lists can contain items of different types. For example, • >>> things = ['one', 2, [3, 4]] • has three items: the first is string 'one', the second is integer 2, and the third item is list [3, 4].

List Operators • For example, the items in the list may be accessed individually using the indexing operator, just as individual characters can be accessed in a string: • >>> pets[0] • 'goldfish' • >>> pets[2] • 'dog' • Negative indexes can be used too: • >>> pets[-1] • 'dog'

Usage and explanation of List operators • Usage 1. x in lst 2. x not in lst 3. lst. A + lst. B 4. lst * n , n * lst 5. lst[i] 6. len(lst) 7. min(lst) 8. max(lst) 9. sum(lst) Explanation True if object x is in list lst, false otherwise. False if object x is in list lst, true otherwise. Concatenation of lists lst. A and lst. B. Concatenation of n copies of list. Item at index i of list lst. Length of list lst. Smallest item in list lst. Largest item in list lst. Sum of items in list lst
![TENET AMMA S=MALAYALAM 0 12345678 X= S[1], Y= S[7] X==Y TRUE TENET AMMA S=MALAYALAM 0 12345678 X= S[1], Y= S[7] X==Y TRUE](http://slidetodoc.com/presentation_image_h2/95e13abbcb5bedaa10e7085f25569ac3/image-47.jpg)
TENET AMMA S=MALAYALAM 0 12345678 X= S[1], Y= S[7] X==Y TRUE

LIST - Defined • List-Definition: • A list is created by placing all the items(elements) inside the square brackets[], seperated by commas. • It can have any number of items and they may be of different types such as integer, float, string etc. • A list can also have another list as an item. This is called Nested list
![How to create List? 1. Empty List: >>> list 1=[] 2. List of integers: How to create List? 1. Empty List: >>> list 1=[] 2. List of integers:](http://slidetodoc.com/presentation_image_h2/95e13abbcb5bedaa10e7085f25569ac3/image-49.jpg)
How to create List? 1. Empty List: >>> list 1=[] 2. List of integers: >>>List=[1, 2, 3, 4, 5] 3. List with mixed Data types: List = [“computer”, [5, 4, 7, 8], [‘x’]]

String and List Excercises 1. Create any five strings for your own example. 2. Concatenate any two strings. 3. Find the substring from the string using “in operator 4. Make multiple copy of the string”hello” by 8 times 5. Compare the given two strings and say whether true or false(use equal, not equal , les than and greater than operators) 1. Happy 2. un happy 6. create a list for 10 integers 7. Create a list for two integers and 3 floats 8. Create a list for 5 elements with the combination of string, integer and float 9. Locate list elements using index operator 10 locate the list element from forward and reverse directions

LISTS DEFINITION AND EXAMPLES In Python programming, a list is created by placing all the items (elements) inside square brackets [], separated by commas. • It can have any number of items and they may be of different types (integer, float, string etc. ). • empty list example: >>>my_list = [] • • • list of integers Example >>>my_list = [1, 2, 3] # list with mixed data types Example my_list = [1, "Hello", 3. 4] A list can also have another list as an item. This is called a nested list Example: my_list = ["mouse", [8, 4, 6], ['a']]
- Slides: 51