Docsity

Python Programming: Unit 1 Overview, Lecture notes of Computer science

Partial preview of the text

Download Python Programming: Unit 1 Overview and more Lecture notes Computer science in PDF only on Docsity!

Programming

LESSON [05] – PYTHON – PART I

Lesson Outline

 Features of Python

 Variables & Assignment

 Operators

 Control constructs - recap

 Break / Continue Keywords

 Else in Loops

 print() Function

 input() Function

Features of Python

 Easy-to-learn, Easy-to-read, Easy-to-maintain:

 A broad standard library: Python's bulk of the library is very portable

 Interactive Mode: Python has support for an interactive mode

 Portable: Python can run on a wide variety of hardware platforms

 Extendable: You can add low-level modules to the Python interpreter.

 Databases: Python provides interfaces to all major commercial databases.

 GUI Programming: Python supports GUI applications

 Scalable: Has a better structure and support for large programs than shell scripting.

Basic rules in Python

 Language is Case Sensitive ( Capital & Simple are different )

 All command are in lower case

 Code blocks are identified by Indentation

 Editor is language sensitive

 Commands appear in – Red
 Functions appear in – Purple
 Strings appear in - Green

Standard Data Types Python has five standard data types:

 Number – Can store integers and fractional numbers

 String – Can store a series of characters

 List – Can store array of objects ( any kind )

 Tuple – Same as list but objects cannot be changed

 Dictionary – List of a pair containing key + value

Operators & Operands

Expression 4 + 5 is equal to 9.

Here 4 and 5 are called operands and + is called operator.

Python language supports following type of operators.

 Arithmetic Operators

 Comparison Operators

 Logical (or Relational) Operators

 Assignment Operators

 Conditional (or ternary) Operators

Comparison Operators

Assume,

variable a holds 10

variable b holds 20

Operator Description Example == Checks if the value of two operands are equal or not, if yes then condition becomes true. (a == b) is not true. != Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a != b) is true. <> Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. (a <> b) is true. This is similar to != operator. > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (a > b) is not true. < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (a < b) is true. >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (a >= b) is not true. <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (a <= b) is true.

Assignment Operators Operator Description Example = Assigns values from right side operands to left side operand c = a + b will assign value of a + b into c += It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a

- = It subtracts right operand from the left operand and assign the result to left operand c - = a is equivalent to c = c – a = It multiplies right operand with the left operand and assign the result to left operand c = a is equivalent to c = c * a /= It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / a %= It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a ****= Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a //= Performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

Operator Example x= y= print("x =",x) print("y =",y) print("x + y =",x+y) print("x - y =",x-y) print("x * y =",xy) print("x / y =",x/y) print("x % y =",x%y) print("x // y =",x//y) print("x ** y =",xy) x += y print("After x+=y, now x =",x) p= q= print() print("With p=",p,"and q=",q,"Some Bitwise and Logical operators") print("p >> 2 =",p>>2) print("p << 3 =",p<<3) print("p & q",p&q) print("p | q",p|q) print("p ^ q",p^q) print("x!=y and p>8",x!=y and p>8) print("x!=y or p>8",x!=y or p>8) print("not p==8",not p==8)*

Control Constructs

Python supports following control constructs.

Chapter 3 we have done them in detail.

 if Statement

 if…elif Statement

 while loop

 for loop

Continue keyword

 The continue statement is used to

skip the rest of the code inside a

loop for the current iteration only.

 Loop does not terminate but

continues on with the next iteration.

Find the output

Use of break statement inside a for loop

for val in "string": if val == "i": break print(val) print("The end")

Else in Loops ( Find the output ) x = 1 while x<= 5 : print(x) x = x + 1 else: print(“The end value of x = “,x) print(“** The End **”)

 In almost every programming

language ‘else’ can be used only

with conditional statements

 Python allows us to use else

statements with loops too.

Else in Loops ( Find the output ) x = 1 while x<= 5 : print(x) x = x + 1 else: print(“The end value of x = “,x) print(“** The End **”)

 In almost every programming

language ‘else’ can be used only

with conditional statements

 Python allows us to use else

statements with loops too.