Tuesday, 30 April 2019

bytearray

Bytearray:


Bytearray is exactly same as bytes data type except its elements can be modified

Syntax:
1. create one list elements
2. type cast list element into bytearray elements

(1)Variable=[no1 ,no2,…]

Variable=bytearray(variable) #Type casting from list elements into bytearray elements

(2) Direct way to create
Variable=bytearray([no1,no2,….])

Example:

>>> a=[10,20,30] #list is created
>>> a=bytearray(a) #type casted from list elements into bytearray elements
>>> for i in a:print(i) #processing each elements where i varies from index 0th element to size-1 #elements.
10
20
30

Example for 2nd way:
a=bytearray([10,20,30])

We can modify the values
a[0]=15 #we can modify

The previous session we have discussed so far:

Data types:
Data type
Description
Is immutable?
Example
int
Used to represent the numbers
Immutable
a=10
float
Used to represent decimal point numbers
Immutable
a=12.5
complex
Used to represent complex nos
Immutable
a=2+4j or a=2+4J
bool
Used to represent the logical values(True/False)
Immutable
a=True
b=False
str
Used to represent collection of characters, should be enclosed within single quote or double quotes
Immutable
S=’ravi’
A=””Vij is a cool city”””
b=”2+%j”
c=’b’
Bytes
Used to represent a collection of byte values from 0 to 255
Immutable
a=bytes([120,20,30])
Bytearraya
Used to represent a collection of byte values from 0 to 255
Mutable
a=bytearray([10,20,30])
Range
Used to represent a range of values
Immutable
r=range(10)
a=range(0,10)
b=range(1,10,3)
List
Used to represent an ordered collection of elements(objcts)
Mutable
a=[10,20,30,40]
Tuple
Used to represent an ordered collection objects (elements)
Immutable
a=(2,4,5,6)
Set
Used to represent an unordered collection unique objects
Mutable
a={1,2,3,4}
Frozenset
Used to represent an unordered collection of objects
Immutable
s={1,2,’ravi’,4}
a=frozenset(s) or a=frosenset({1,2,’ravi’,4})
Dict
Used to represent a group of key and value pair
Mutable
a={10:’ravi’,20:’sita’}
None
Used to represent no value or Nothing

def  sum():
   print(“hi”)
print(sum())
output: None




Constants in Python:

Defn: A constant is similar to a variable but its value can not be modified or changed in the course of the program execution.
The constant concept is not applicable in Python. It is possible in Java, C++ and C languages.
But A developer can indicate a variable as constant by writing its name in all upper case letters. 
For our convenience we use uppercase characters.

Example:
In mathematics, 'PI' value is 3.1714 which never changes and it is constant. 

PI=3.1714  #It is just convention but we can change the value.

Next topic: Operators
Previous topic: byte data type

You may like the following topic:


Monday, 29 April 2019

if…elif..else statement

if…elif..else:

Example 1
the marks obtained by a student in 3 subjects are input through the keyboard.
the students get a division as per the following rules:
75-100 distinction, 60--74 first class,50-59:2nd class,40--49:3rd class

#m,p,c=int(input("enter m,p,c marks"))#errr
m,p,c=(int(x) for x in input("enter m,p,c marks:").split())
avg=m+p+c/3;
if avg>=75:
    print("u passed in distinction %= ",avg)
elif avg>=60:
    print("U passed in First class %=",avg)
elif avg>=50:
    print("U passed in 2nd class %=",avg)
elif avg>=40:
    print("U passed in 3rd class %=",avg)
else:
    print("Failed,%=",avg)

output:
enter m,p,c marks:20 10 34
U passed in 3rd class %= 41.333333333333336
>>> 
Definition:

When we want to test multiple conditions and execute statements depending on those conditions. "if..elif..else statement" is useful.

Syntax:

if condition1:
    Statement
elif condition2:
    Statement2
elif conditon3:
   Statement3
….
else
    Default statement.

Based on the condition the corresponding action will be executed.

Explanation: condition 1 is True: statement 1 is executed
Remaining statements are not executed.
If condition 2 is True: only statement 2 is executed

If condition 3 is True: only statement 3 is executed

Ex2:

#Largest of two nos
a=int(input("enter 1st no"))
b=int(input("enter 2nd no"))

#Logic
if a>b:
    print("a is big")
else:
    print("b is big")
"""
output
enter 1st no2
enter 2nd no3
b is big
>>> """

Ex3:

#find the Largest of 3 nos
a=int(input("Enter 1st no"))
b=int(input("enter 2nd no"))
c=int(input("enter 3rd no"))

#Logic
if a>b and a>c:
    print("Big no is a=",a)
elif b>c:
    print("Big no is b=",b)
else:
    print("Big no is c=",c)
"""
Note1:
Enter 1st no 10
enter 2nd no 20

enter 3rd no 30

Note2:
Enter 1st no 40
enter 2nd no 20
enter 3rd no 5
Big no is a= 40
>>>
"""

Next Topic:  Loops in Python
Previous Topic: if else statement

Files with Exception handling

#Ask the user to take two input integer values var a,b try:     a=int(input("enter any integer value:"))     b=int(input(...