0% found this document useful (0 votes)
4 views26 pages

Basic Operations (String & Integer)

This lecture covers basic operations in Python, focusing on strings, their properties, and various types of operators including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains string manipulation techniques such as slicing and concatenation, as well as data type conversion. The document provides examples for each operator type to illustrate their usage.

Uploaded by

Tahir Jutt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views26 pages

Basic Operations (String & Integer)

This lecture covers basic operations in Python, focusing on strings, their properties, and various types of operators including arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators. It explains string manipulation techniques such as slicing and concatenation, as well as data type conversion. The document provides examples for each operator type to illustrate their usage.

Uploaded by

Tahir Jutt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 02

Python – Basic Operations


Lecture 02
Python – Basic Operations

Number and Types


STRING
▪ Any data type surrounded by quotation mark (either
single or double quotation) is coined as string.

MULTILINE STRING
▪ Multiline string need multi quotation.
STRING LENGTH
▪ String length includes the alphabets and spaces as
well.
CHECK STRING
▪ You can check string if it’s present or not by using
keyword ‘in’
Lecture 02
Python – Basic Operations

String Operator
>>> s = 'a\nb\tc'
>>> s
'a\nb\tc'
>>> print(s)
a
bc
>>> S = 'Spam' # Make a 4-character
string, and assign it to a name
>>> len(S) # Length
4
>>> S[0] # The first item in S, indexing
by zero-based position
'S'
Lecture 02
Python – Basic Operations

Slicing
>>> S[1:3] # Slice of S from offsets 1 through 2 (not 3)
'pa'
>>> S[1:] # Everything past the first (1:len(S))
'pam'
>>> S # S itself hasn't changed
'Spam'
Strings are immutable in Python i.e. they cannot be changed in place after
they are created. For example, a string can‘t be changed by assigning to
one of its positions, but new string can always be assigned to the same
string. Because Python cleans up old objects
>>> S
'Spam'
>>> S[0] = 'z' # Immutable objects cannot be changed
...error text omitted...
Lecture 02
Python – Basic Operations

>>> S[0] = 'z' # Immutable objects cannot be changed


...error text omitted...
TypeError: 'str' object does not support item assignment
>>> S = 'z' + S[1:] # But we can run expressions to make new
objects
>>> S
'zpam'
>>> 'abc' + 'def' # Concatenation: a new string
'abcdef'
>>> 'Ni!' * 4 # Repetition: like "Ni!" + "Ni!" + ...
'Ni!Ni!Ni!Ni!'
Lecture 02
Python – Basic Operations

▪ In Python, we can also index backward, from the end—positive indexes count from
the left, and negative indexes count back from the right:
>>> S[-1] # The last item from the end in S
'm'
>>> S[-2] # The second-to-last item from the end
>>> S # A 4-character string
'Spam'
Lecture 02
Python – Basic Operations

Extended Slicing
The third parameter in square #Output:retupmoC
bracket defines a=s[1:5:1]
Difference between the indexes print(a)
to be printed on output # Output:ompu
Direction of access i.e. negative a=s[1:5:2]
difference define the access print(a)
direction from right to left # Output:op
a=s[5:1:-1]
s='Computer' print(a)
a=s[::-1] # Output:tupm
print(a)
Lecture 02
Python – Basic Operations

Data Type Conversion


>>> "42" + 1
TypeError: Can't convert 'int' object to str implicitly
>>> int("42"), str(42) # Convert from/to string
(42, '42')
>>> S = "42"
>>> I = 1
>>> S + I
TypeError: Can't convert 'int' object to str implicitly
>>> int(S) + I # Force addition
43
>>> S + str(I) # Force concatenation
'421'
Lecture 02
Python – Basic Operations

Python Operators
Python divides the operators in the following groups:
▪ Arithmetic operators
▪ Assignment operators
▪ Comparison operators
▪ Logical operators
▪ Identity operators
▪ Membership operators
▪ Bitwise operators
Lecture 02
Python – Basic Operations

Arithmetic Operators
▪ It deals with numeric values and common mathematical operations
Operator Name Example
+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Floor division x // y
Lecture 02
Python – Basic Operations

Assignment Operators
▪ It assign values and assess in operations
Operator Example Same As

= x=5 x=5

+= x += 3 x=x+3

-= x -= 3 x=x-3

*= x *= 3 x=x*3

/= x /= 3 x=x/3

%= x %= 3 x=x%3

//= x //= 3 x = x // 3
Lecture 02
Python – Basic Operations

**= x **= 3 x = x ** 3

&= x &= 3 x=x&3

|= x |= 3 x=x|3

^= x ^= 3 x=x^3

>>= x >>= 3 x = x >> 3

<<= x <<= 3 x = x << 3


Lecture 02
Python – Basic Operations

Comparison Operators
▪ Comparison between two values can be made by using comparison operators.
Operator Name Example
== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


Lecture 02
Python – Basic Operations

Logical Operators
▪ Conditional statements requires logical operators.

Operator Description Example

and Returns True if both statements x < 5 and x < 10


are true

or Returns True if one of the x < 5 or x < 4


statements is true

not Reverse the result, returns False if not(x < 5 and x < 10)
the result is true
Lecture 02
Python – Basic Operations

Identity Operators
▪ Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
Operator Description Example

is Returns True if both variables are x is y


the same object

is not Returns True if both variables are x is not y


not the same object
Lecture 02
Python – Basic Operations

Identity Operators
▪ Identity operators are used to compare the objects, not if they are equal, but if they are
actually the same object, with the same memory location:
Operator Description Example

is Returns True if both variables are x is y


the same object

is not Returns True if both variables are x is not y


not the same object
Lecture 02
Python – Basic Operations

Membership Operators
▪ Membership operators are used to test if a sequence is presented in an object:

Operator Description Example

in Returns True if a sequence with x in y


the specified value is present in
the object

not in Returns True if a sequence with x not in y


the specified value is not
present in the object
Lecture 02
Python – Basic Operations

Bitwise Operators
▪ It is used in binary number operations.
Operat Name Description
or
& AND Sets each bit to 1 if both bits are 1
| OR Sets each bit to 1 if one of two bits is 1
^ XOR Sets each bit to 1 if only one of two bits is 1
~ NOT Inverts all the bits
<< Zero fill Shift left by pushing zeros in from the right and let the
left shift leftmost bits fall off
>> Signed Shift right by pushing copies of the leftmost bit in from
right shift the left, and let the rightmost bits fall off
Lecture 02
Python – Basic Operations

Python Operators
▪ Operations are mainly performed on values and variables.
▪ Routine mathematical operations like subtraction, multiplication and division can be
performed in the similar way as addition operation performed below:
>>> 123+456 #Addition
579
>>> 123**2 #Power
15129
>>> 2.0 >= 1 # Greater than or equal: mixed-type 1 converted to 1.0
True
>>> 2.0 == 2.0 # Equal value
True
>>> 2.0 != 2.0 # Not equal value
False
Lecture 02
Python – Basic Operations

Basic arithmetic operator examples

Add

Subtract

Multiply

Divide

Floor Division

Exponent
Lecture 02
Python – Basic Operations

Comparison operator examples


Greater than

Less than

Equal to

Not Equal to

Greater than or equal to

Less than or equal to


Lecture 02
Python – Basic Operations

Logical operator examples

And

Or

Not
Lecture 02
Python – Basic Operations

Identity operator examples

Is

Is not
Lecture 02
Python – Basic Operations

Membership operator examples

In

Not in
Lecture 02
Python – Basic Operations

Square Root

OUTPUT
Lecture 02
Python – Basic Operations

Thank you!

You might also like