Python
Python
Data types define the type of value a variable can hold in a programming
language.
In python , type() function is used to check the data type of a variable or value.
1. Numeric
It represent the data that has a numeric value.
A numeric value can be an integer, a floating number or a complex number.
Integers (int): Whole numbers, positive or negative, without decimals, of unlimited length
Float (float): Floating point numbers
Complex Numbers (complex): Numbers with real and imaginary parts
x = 1 # int
y = 2.8 # float
z = 3+4 j # complex
a=True
type(a)
<class ‘bool’>
3. String
Collection of one or more characters put in a single quote, double quote or triple quote
String data type is represented as 'str'
a = "Hello"
b = ‘Hello’
c = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
a = "Hello, World!"
print(a[0])
capitalize() Converts the first character to upper
case
casefold() Converts string into lower case
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
To change tuple values
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
To add items:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.append("orange")
thistuple = tuple(y)
Or
thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
To join two or more tuples you can use the + operator:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
To multiply the content of a tuple a given number of times, you can use the * operator:
fruits = ("apple", "banana", "cherry")
mytuple = fruits * 2
print(mytuple)
thisdict = {
"name": "maya",
"age": "20",
"id": 1964
}
print(thisdict["age"])
7. Set
Sets are used to store multiple items in a single variable.
A set is a collection which is unordered, unchangeable, unindexed and do not allow
duplicate values.
Sets are written with curly brackets.
thisset = {"apple", "banana", "cherry"}
print(thisset)
Cannot access items in a set by referring to an index or a key.But can loop through the
set items using a for loop.
thisset = {“apple”, “banana”, “cherry”}
for x in thisset:
print(x)
add() – Adds an element to the set
myset ={“a”, “b”, “c”}
myset.add(“d”)
print(myset)
clear() – Removes all the elements from the set
copy() – Returns a copy of the set
Union() - | - Return a set containing the union of sets
people = {“Jay”, “Idrish”, “Archil”}
vampires = {“Karan”, “Arjun”}
population = people.union(vampires)
OR
population = people|vampires
Intersection() - & - Returns a set, that is the intersection of two other
sets
difference() - - - Returns a set containing the difference between
two or more sets
remove() – Removes the specified element