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:


1 comment:

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(&qu...