🔥 Welcome to my blog! Latest updates available now. Visit daily! 🔥 🔥 Click Here for Latest Updates – Visit My Blog 🔥
🔥 Latest Updates 📊 GP Grade Finder 📢 Government Services Info 🆕 New Schemes Updates
Showing posts with label Programming Languages. Show all posts
Showing posts with label Programming Languages. Show all posts

range Data Type in Python

-->range Data Type represents a sequence of numbers.

-->The elements present in range Data type are not modifiable. i.e range Data type is immutable.


Form-1: range(10)#generate numbers from 0 to 9

Example:

rarange(10)

for i in r: print(i) 0 to 9


Form-2; range(10,20)#generate numbers from 10 to 19

r = range(10,20)

for i in r: print(i) 10 to 19


Form-3: range(10,20,2)#2 means increment value.

r = range(10,20,2)

for i in r: print(i) #10,12,14,16,18


-->We can access elements present in the range Data Type by using index.


r=range(10,20)

r[0]==>10

r[15]==>IndexError: range object index out of range

-->We cannot modify the values of range data type

Example:

r[0]=100

TypeError: 'range' object does not support item assignment.

-->We can create a list of values with range data type.

Example

>>> l=list(range(10))

>>>l#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

set Data Type in Python

If we want to represent a group of values without duplicates where order is not important, then we should go for set Data Type.


1. insertion order is not preserved

2. duplicates are not allowed

3. heterogeneous objects are allowed

4. index concept is not applicable

5. It is mutable collection

6. Growable in nature

Example:

1) s=(100,0,10,200,10,'siddu')# (0, 100, 'siddu', 200, 10)


s[0]=>TypeError: 'set' object does not support indexing.

set is growable in nature, based on our requirement we can increase or decrease the size.


>>>s.add(60)

>>>s#(0, 100, 'siddu', 200, 10, 60)

>>> s.remove(100)

>>> s#(0, 'siddu', 200, 10, 60)

frozenset Data Type in Python

It is exactly same as set except that it is immutable. 

Hence we cannot use add or remove functions.


>>> s=(10,20,30,40)

>>> fs-frozenset(s)

>>> type(fs)

<class 'frozenset'> 5) >>> fs

frozenset((40, 10, 20, 30))

>>> for i in fs:print(i)

40 10 20 30

>>> fs.add(70)

AttributeError: 'frozenset' object has no attribute 'add' 

>>> fs.remove(10)

AttributeError: 'frozenset' object has no attribute 'remove'


dict Data Type In Python

 If we want to represent a group of values as key-value pairs then we should go for dict data type.


Example:

d=(101:'siddu',102:'ravi',103:'chiru')


Duplicate keys are not allowed but values can be duplicated.


If we are trying to insert an entry with duplicate key then old value will be replaced with new value.


Example:

>>> d=(101:'siddu',102:'ravi", 103:"chiru"}

>>> d[101]='lakshmi'

>>> d

(101: 'lakshmi', 102: 'ravi', 103: 'chiru')


We can create empty dictionary as follows d=()

We can add key-value pairs as follows d['a']='apple'

d['b']='banana'

print(d)


Note: dict is mutable and the order wont be preserved.

Type Casting in Python Programming

Ø  We can convert one type value to another type. This conversion is called Typecasting or Type coercion.

The following are various inbuilt functions for type casting.

1. int()                        2. float()         3. complex()              4. bool()         5. str()

1. int();

Ø  We can use this function to convert values from other types to int

Example:

>>> int(123.987)à123

>>> int(10+5) à TypeError: can't convert complex to int

>>> int(True)à 1

>>> int(False)à 0

>>> int("10")à10

>>> int("10.5")à ValueError: invalid literal for int() with base 10: "10.5"

>>> int("ten")à ValueError: invalid literal for int() with base 10: "ten'

>>> int("OB1111")àValueError: invalid literal for int() with base 10: '081111'

Note:

1. We can convert from any type to int except complex type.

2. If we want to convert str type to int type, compulsary str should contain only integral value and should be specified in base-10

2. float():

Ø  We can use float() function to convert other type values to float type.

 >>> float(10)à 10.0

>>> float(10+5)) àTypeError: can't convert complex to float

>>> float(True)à 1.0

>>> float(False)à 0.0

>>> float("10")à10.0

>>> float("10.5")à10.5

>>> float("ten")à ValueError: could not convert string to float: "ten'

>>> float("081111")àValueError: could not convert string to float: '081111'

Note:

1. We can convert any type value to float type except complex type.

2. Whenever we are trying to convert str type to float type compulsary str should be either integral or floating point literal and should be specified only in base-10.

3.complex();

Ø  We can use complex() function to convert other types to complex type.

Form-1: complex(x)

Ø  We can use this function to convert x into complex number with real part x and imaginary part 0.

Example:

complex(10) è10+0j

complex(10.5) è10.5+0)

complex(True) è1+0j

complex(False) è0j

complex("10")è10+0j

complex("10.5")è10.5+0]

complex("ten")è ValueError: complex() arg is a malformed string

Form-2: complex(x,y)

Ø  We can use this method to convert x and y into complex number such that x will be real part and y will be imaginary part.

Example: complex(10,-2)è10-2j

                   complex(True,False)è1+0j

4. bool():

Ø  We can use this function to convert other type values to bool type.

Example:

>>> bool(0)èFalse

>>> bool(1) èTrue

>>> bool(10) èTrue

>>> bool(10.5) èTrue

>>> bool(0.178) èTrue

>>> bool(0.0) èFalse

>>> bool(10-2)) èTrue

>>> bool(0+1.5j) èTrue

>>> bool(0+0)) èFalse

>>> bool("True")èTrue

>>> bool("False")èTrue

>>> bool("")èFalse

 

5. str();

Ø  We can use this method to convert other type values to str type

Example:

>>> str(10) è'10'

>>> str(10.5) è10.5"

>>> str(10+5) è(10+5))"

>>> str(True) è 'True'

Data Types in Python Programming

Data Types

Ø  Data Type represents the type of data present inside a variable.

Ø  In Python we are not required to specify the type explicitly.

Ø  Based on value provided, the type will be assigned automatically.

Ø  Hence Python is Dynamically Typed Language.

Python contains the following inbuilt data types

1. int

4.bool

7.bytearray

10.tuple

13.dict

2. float

5.str

8.range

11.set

14. None

3.complex

6.bytes

9.list

12.frozenset

 


Note: Python contains several inbuilt functions

1. type():To check the type of variable

2. id():To get address of object

3. print():To print the value.

In Python everything is an object

int Data Type:

Ø  We can use int data type to represent whole numbers (integral values)

Example: >>> a=10                            >>> a=40+60

               >>> type(a)                          >>> a

               <class 'int'>                          100

Note:

Ø  In Python2 we have long data type to represent very large integral values.

Ø  But in Python3 there is no long type explicitly and we can represent long values also by using int type only.

We can represent int values in the following ways

1. Decimal form

2. Binary form

3. Octal form

4. Hexa decimal form

1. Decimal form(base-10):

Ø  It is the default number system in Python.The allowed digits are: 0 to 9

Example : >>> d=528                          >>> d=1234a

                  >>> d                                >>> d=1234a

                        528                                     File "<stdin>", line 1    d=1234a         

                                                                                                             ^SyntaxError: invalid syntax

  2. Binary form(Base-2);

Ø  The allowed digits are: 0 & 1.Literal value should be prefixed with Ob or OB

Example :>>> b=0b1111                    >>> b=0B123

                >>> b                                 File "<stdin>", line 1    b=0B123        

                       15                                ^SyntaxError: invalid digit '2' in binary literal3

3. Octal Form(Base-8);

Ø  The allowed digits are: 0 to 7,Literal value should be prefixed with 00 or 00.

Example: >>> o=0o1234      >>> o=0O143

                 >>> o                    >>> o

                        668                       99

              >>> o=0O148

                     File "<stdin>", line 1    o=0O148

                                                                        ^SyntaxError: invalid digit '8' in octal literal

4. Hexa Decimal Form(Base-16);

Ø  The allowed digits are: 0 to 9, a-f (both lower and upper cases are allowed) Literal value should be prefixed with Ox or OX

Example:>>> h=0x123f                            >>> h=0X123e

               >>> h                                        >>> h

                      4671                                           4670

>>> h=0X14h

  File "<stdin>", line 1    h=0X14h

                                                     ^SyntaxError: invalid syntax

Note: Being a programmer we can specify literal values in decimal, binary, octal and hexa decimal forms. But PVM will always provide values only in decimal form.

2.float Data Type:

Ø  We can use float data type to represent floating point values (decimal values)

Example :.>>> f=1.234                >>> f = 1.2e3      >>> f = 3.4E2    >>> type(f) 

               >>> f                            >>> f                     >>> f                      <class 'float'>

                      1.234                             1200.0                   340.0

Ø  We can also represent floating point values by using exponential form (scientific notation

Ø  Instead of 'e' we can use 'E'

Ø  The main advantage of exponential form is we can represent big values in less memory.

Note:

Ø  We can represent int values in decimal, binary, octal and hexa decimal forms. But we can represent float values only by using decimal form.

>>> f=0B11.01

  File "<stdin>", line 1    f=0B11.01         

                                                    ^Syntax Error: invalid syntax

x = 35e3

y = 12E4

z = -87.7e100

print(type(x))     #<class 'float'>

print(x)              #35000.0

print(type(y))   #<class 'float'>

print(y)           #120000.0

print(type(z))   #<class 'float'>

print(z)            #-8.77e+101

3. complex Data Type:

Ø  A complex number is of the form

a           +     bj

Ø  Real Part Imaginary Part

Ø  a and b contain integers or floating point values

Examples :3+5j 10+5.5j  0.5+0.1j

Ø  In the real part if we use int value then we can specify that either by decimal,octal,binary or hexa decimal form.

Ø  But imaginary part should be specified only by using decimal form.

>>> a=0B11+5j

 >>> a

 (3+5j)

 >>> a=3+0B11j

 Syntax Error: invalid syntax

Ø  Even we can perform operations on complex type values.

1) >>> a=10+1.5j

 2) >>> b=20+2.5j

 3) >>> c=a+b

4) >>> print(c)

5) (30+4))

6) >>> type(c)

7) <class 'complex'>

Note: Complex data type has some inbuilt attributes to retrieve the real part and imaginary part

                                c=10.5+3.6j

c.real==>10.5                                 c.imag==>3.6

Ø  We can use complex type generally in scientific Applications and electrical engineering Applications.

4. bool Data Type:

Ø  We can use this data type to represent boolean values. The only allowed values for this data type are:True and False

Ø  Internally Python represents True as 1 and False as 0

b=True

type(b)

 =>bool

a=10

b=20

c=a<b

print(c)==>True

True+True==>2

True-False==>1

5. str Data Type:

Ø  str represents String data type.

Ø  A String is a sequence of characters enclosed within single quotes or double quotes.

s1='siddu' s1="siddu"

Ø  By using single quotes or double quotes we cannot represent multi line string literals.

s1="siddu

                                 soft"

Ø  For this requirement we should go for triple single quotes("") or triple double quotes(""")

            s1=’’’siddu soft’’’   s1="""siddu soft"""

Ø  We can also use triple quotes to use single quote or double quote in our String.

             ""This is " character"" 

             'This is" Character'

Ø  We can embed one string in another string ""This "Python class very helpful" for java students""

ü  In Python the following data types are considered as Fundamental Data types

             1. int      2. float          3. complex                   4. bool             5 .str

ü  In Python, we can represent char values also by using str type and explicitly char type is not available.

Example:

 >>> c='a'

 >>> type(c)

 <class 'str'>

ü  long Data Type is available in Python2 but not in Python3. In Python3 long values also we can represent by using int type only.

ü  In Python we can present char Value also by using str Type and explicitly char Type is not available.

à°†ంà°§్à°°à°ª్à°°à°¦ేà°¶్‌à°²ో సదరం సర్à°Ÿిà°«ిà°•ేà°Ÿ్ à°•ోà°¸ం à°Žà°¦ుà°°ుà°šూà°¸్à°¤ుà°¨్à°¨ à°¦ిà°µ్à°¯ాంà°—ులకు à°¸్à°²ాà°Ÿ్ à°¬ుà°•ింà°—్ à°…à°ª్‌à°¡ేà°Ÿ్.

à°‡ంతకుà°®ుంà°¦ు వరకు SADAREM Slot Booking à°ª్à°°à°•్à°°ిà°¯ à°¸్వర్à°£ à°—్à°°ాà°®ం à°¸్వర్à°£ à°µాà°°్à°¡ు à°¦్à°µాà°°ా à°®ాà°¤్à°°à°®ే à°…ంà°¦ుà°¬ాà°Ÿుà°²ో à°‰ంà°¡ేà°¦ి. à°ª్à°°à°¸్à°¤ుà°¤ం à°ª్రజలకు మరింà°¤ à°¸ౌà°•à°°్యవం...