Sunteți pe pagina 1din 4

Data type Description Example

Str String literals in python are x = "Hello World"


surrounded by either single
quotation marks, or double
quotation marks.
int It is a whole number,positiveor x = 20
negative, without decimals, of
unlimited length.
Float It is number positive or x = 20.5
negative, containing one or
more decimals.
Complex Complex numbers are written x = 1j
with a “j” as the imaginary parts
List It is a collection which is x = ["apple", "banana",
ordered and changeable. Allowa "cherry"]
duplicate members.
Tuple It is a collection which is x = ("apple", "banana",
ordered and unchangeable. "cherry")
Allows duplicate members.
range The range() function returns a x = range(6)
sequence of numbers, starting
from 0 by default, and
increments by 1(by default),and
ends at a specified number.
dict It is a collection which is x = {"name" : "John", "age" :
unordered, changeable and 36}
indexed. No duplicate members.
Set It is a collection which is x = {"apple", "banana",
unordered and unindexed. No "cherry"}
duplicate members.
frozenset It returns an unchangeable x = frozenset({"apple",
frozenset object(which is like a "banana", "cherry"})
set object, only unchangeable).
Bool It represent one of two values: x = True
true or false.
bytes It can convert objects into bytes x = b"Hello"
objects, or create empty byte
object of the specified size.
bytearray It can convert object into x = bytearray(5)
bytearray objects, or create
empty bytearray object of the
specified size.
memoryview It returns a memory view object x = memoryview(bytes(5))
from a specified object.

Range() :
The range() is a built-in function of Python which returns a range object, which is nothing but a
sequence of integers. i.e., Python range() generates the integer numbers between the given start
integer to the stop integer, which is generally used to iterate over with for loop. 
Syntax:
range (start, stop[, step])
 range() takes three arguments.
 Out of the three 2 arguments are optional. I.e., Start and Step are the optional arguments.
1. A start argument is a starting number of the sequence. i.e., lower limit. By default, it starts
with 0 if not specified.
2. A stop argument is an upper limit. i.e.generate numbers up to this number, The range() 
function doesn’t include this number in the result.
3. The step is a difference between each number in the result. The default value of the step is
1 if not specified.

Frozenset

Frozen set is just an immutable version of a Python set object. While elements of a set can be
modified at any time, elements of frozen set remains the same after creation.
mylist = ['apple', 'banana', 'cherry']
x = frozenset(mylist)
x[1] = "strawberry"

Traceback (most recent call last):


  File "demo_ref_frozenset2.py", line 3, in <module>
    x[1] = "strawberry"
TypeError: 'frozenset' object does not support item assignment

Set

add(element)

A method which adds an element, which has to be immutable, to a set.


>>> colours = {"red","green"}
>>> colours.add("yellow")
>>> colours
set(['green', 'yellow', 'red'])

remove(el)

works like discard(), but if el is not a member of the set, a KeyError will be raised.
>>> x = {"a","b","c","d","e"}
>>> x.remove("a")
>>> x
set(['c', 'b', 'e', 'd'])

pop()

pop() removes and returns an arbitrary set element. The method raises a KeyError if
the set is empty
>>> x = {"a","b","c","d","e"}
>>> x.pop()
'a'
bool() in Python

The bool() method is used to return or convert a value to a Boolean value i.e., True or False,
using the standard truth testing procedure.
Syntax:

bool([x])

The bool() method in general takes only one parameter(here x), on which the standard truth
testing procedure can be applied. If no parameter is passed, then by default it returns
False. So, passing a parameter is optional. It can return one of the two values.

 It returns True if the parameter or value passed is True.


 It returns False if the parameter or value passed is False.

Here are few cases, in which Python’s bool() method returns false. Except these all other
values return True.

 If a False value is passed.


 If None is passed.
 If an empty sequence is passed, such as (), [], ”, etc
 If Zero is passed in any numeric type, such as 0, 0.0 etc
 If an empty mapping is passed, such as {}.
 If Objects of Classes having __bool__() or __len()__ method, returning 0 or False

# Python program to illustrate


# built-in method bool()

# Returns False as x is False


x = False
print(bool(x))

# Returns True as x is True


x = True
print(bool(x))

# Returns False as x is not equal to y


x=5
y = 10
print(bool(x==y))

# Returns False as x is None


x = None
print(bool(x))

# Returns False as x is an empty sequence


x = ()
print(bool(x))

# Returns False as x is an emty mapping


x = {}
print(bool(x))
# Returns False as x is 0
x = 0.0
print(bool(x))

# Returns True as x is a non empty string


x = 'GeeksforGeeks'
print(bool(x))

S-ar putea să vă placă și