Python Revision Tour I : Basics of Python – Notes

Tuples

A Tuple is a set of comma-separated values of any data type inside the parentheses (  )

Example =>  (1, 2 , 3 , 4, 5)  

    (1.5, 2.5, 4.5, 9.8) 

    (‘x’,  ‘y’, ‘z’)

    (‘anjeev’, 25,  4589.36, True )

Tuple can also be accessed through the index position i.e. forward and backward.

Dictionary

A dictionary is a comma-separated key-value pairs within { } and accessed using keys.

No two keys can be the same .

Example :   mon ={‘j’: ‘Jan’, ‘f’: ‘Feb’, ‘m’: ‘Mar’}

      mon[‘j’]    => Jan

      values = { ‘a’:  1, ‘e’: 2, ‘i’: 3, ‘o’: 4,  ‘u’ : 5 }

Mutable and Immutable Data Types

Immutable Types

Immutable types are those that can never change their value in place.

The following are immutable:  integers, floating point numbers, Boolean, strings, tuples

In immutable types, the variable names are stored as a reference to a value object, each time when we change the value, the variable’s reference memory address changes.

Note : use id( ) method to get the memory address/reference.

Mutable Types

The Mutable types are those whose values can be changed in place. The following are mutable :  lists, dictionaries, and sets

Operators and Its Types

Arithmetic Operator

OperatorDescription
+ (unary)Explicitly express a +ve number. Example : +3, +98.36
– (unary)Explicitly express a –ve number. Example : -3, -369.36
+ (binary)Add two numbers, Example: 9 + 6 is 15
– (binary)Subtract one value from another number. Example: 9 – 3 is 6
*Product of two numbers. Example 9 * 6 is 54
/Gives a quotient, when one value is divided by the other. Example 9/ 6 is 1.5
// (floor division)Gives the integer part of the quotient, when one value is divided by the other. Example 9//6 is 1.
% (Remainder)Gives remainder when one value is divided by the other. Example 9 % 6 is 3
** (Exponent)To raise a number to the power of another number. For example, 3**2 is 9
Arithmetic Operators | www.mycstutorial.in

Assignment Operator

OperatorDescription
=Called Assignment operator. Assign a value to the variable. Example, x = 20.
+=Add and Assign, Add the R-Value to the L-Value and assign result to L-value. Example x=20,  x += 30  it means x = x + 30  so the new value of x is 50.
-=Subtract and Assign, Subtract the R-Value from L-Value and assign result to L-value. Example x=50,  x -= 30  it means x = x – 30  so the new value of x is 20.
*=Multiply and Assign,  Multiply the R-Value with L-Value and assign product to L-value. Example x=50,  x *= 3 it means x = x * 3  so the new value of x is 20.
/=Divide and Assign Quotient, Divide the L-value with R-Value and assign quotient to L-value. Example, x = 50, x /= 6  its means x = x / 6, and the new value of x is 8.33333……
//=Floor and assign, Divide the L-value with R-Value and assign integer quotient (floor division) to L-value. Example,  x = 50, x //= 6  its means x = x // 6, and the new value of x is 8.
%=Divide and Assign Remainder, Divide the L-value with R-Value and assign remainder to L-value. Example,  x = 50, x %= 6  its means x = x % 6, and the new value of x is 2.
**=Exponent and Assign, Calculate (L-Value)R-Value and assign the result to L-value. Example, x = 3, x**=2  its means x = x2 and the result of x is 9
Assignment Operator | mycututorial.in

Relational Operator

OperatorDescription
==  (Equality)Compares two values for equality, Returns True if they are equal, otherwise returns False.
!=  (Not equal)Compares two values for inequality, Returns True if they are not equal, otherwise returns False.
<  (Less than)Compares two values, Returns True if first value is less than the second, otherwise returns False.
> (greater than)Compares two values, Returns True if first value is greater than the second, otherwise returns False.
<= (Less than or equal to)Compares two values, Returns True if first value is less than or equal to the second, otherwise returns False.
>= (greater than or equal to)Compares two values, Returns True if first value is greater than or equal to the second, otherwise returns False.
Relational Operator | mycstutorial.in

Logical Operator

OperatorDescription
notNegate a condition and  Returns True if the condition is false otherwise returns False.
andCombines two conditions and returns True if both the conditions are true, otherwise returns False.
orCombines two conditions and returns True if at least one of the condition is true, otherwise returns False.
Logical Operator | mycstutorial.in

Bitwise Operator

OperatorDescription
& (bitwise and)The AND (&) operator compares two bits and generates a result of 1 if both bits are 1 otherwise, it returns 0
| (bitwise or)The OR (|) operator compares tow bits and generate a result 1 if the bits are complementary, otherwise it returns 0.
^ (bitwise xor)The EXCLUSIVE-OR (XOR) operator compares two bits and return 1 if either of the bits are 1 and it gives 0 if both bits are 0 or 1
~ (bitwise complement)The COMPLEMENT operator is used to invert all of the bits of the operand.
Bitwise Operator | mycstutorial.in

Note: bin( ) returns binary representation number.

Membership Operator

OperatorDescription
inReturns True if the given value is available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False.
not inReturns True if the given value is not available in the sequence (like sting, tuples, lists, dictionary, etc.) otherwise returns False.
Membership Operators | www.mycstutorial.in

Identity Operator

OperatorDescription
isReturns True, if both operands have the same identity i.e. memory reference, otherwise return False
Is notReturns True, if both operands have not the same identity i.e. memory reference, otherwise return False
Identity Operators | www.mycstutorial.in

Operator Precedence  :

Execution of the operator depends on the precedence of the operator. 

OrderOperatorDescription
Highest()Parentheses
**Exponentiation
|~Bitwise nor
|+, –Unary plus and Minus
|*, /, //, %Multiplication, Division, floor division, remainder
|+, –Addition, Subtraction
|&Bitwise and
|^Bitwise XOR
||Bitwise OR
|<, <=, >, >=, <>, !=, ==, is, is notRelational Operators, Identity Operators
|notBoolean NOT
andBoolean AND
LowestorBoolean OR
Operator Precedence | www.mycstutorial.in

Expression

In Python, An expression is a valid combination of operators, literals, and variables.

There are three types of  Expression in Python, These are-

Arithmetic Expressions:-  combination of integers, floating-point numbers, complex numbers, and arithmetic operators.  Example; 2+9, 9.2/3

Relational Expression :-  combination of literals and/or variables of any valid type and relational operators . Example; x > y, 5 < 8, z == y

Logical Expression:- combination of literals, variables and logical operators. Example;  t and q,   p or g

String Expression :- combination of string literals and string operators ( + -> concatenate, and  * -> replicate). Example; 

  “Hello” + “Anjeev” = “Hello Anjeev”

  “Like” * 3 =  “LikeLikeLike”

Types Casting / Explicit Type Conversion

The conversion of one type to a specific type explicitly, is called Type Casting / Explicit Type Conversion.

Syntax:  <type>(  )

Example  x = int ( “123”)

    y = float(“5369.36”)

    z = str(598698.36)

Comments in Python

Comments are a non-executable statement in Python, used for the documentation of the program i.e. specify the purpose of the program, the purpose of expression, variables, statements, blocks, etc.

There are two types of Comments in Python –

•Full Line or Inline Comment

  # symbol is used to write a single or inline comment in Python ->full line comment

  r = 20  # rate of interest -> inline comment

•Multiline Comments or Block Comments

  ‘’’   ‘’’    or  “””  “””  – triple quotes (single or double) are used for multiline comments in python..


Click here for –

Python Revision Tour – I (B) : Decision-Making Statements

Python Revision Tour – I (C): Looping & Jump Statements


Class 12 Computer Science



Leave a Comment

You cannot copy content of this page

Scroll to Top