Data Types and Typecasting in Python (DAY-4)
Data Types and Typecasting in Python (DAY-4)
A data type is a classification that specifies which type of value a variable holds. Python has several
built-in data types that help programmers store, manipulate, and process different kinds of data
effectively.
Integer (int)
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
Float (float)
Represents real numbers with a decimal point. "floating point number" is a number, positive or
negative, containing one or more decimals. Float can also be scientific numbers with an "e" to
indicate the power of 10
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Complex (complex)
Represents complex numbers with real and imaginary parts. Complex numbers are written with a "j"
as the imaginary part.
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Boolean(bool)
You can evaluate any expression in Python, and get one of two answers, True or False.
When you compare two values, the expression is evaluated and Python returns the Boolean answer
x = bool(5)
print(x) #display x:
print(10 > 9)
print(10 == 9)
print(10 < 9)
Any list, tuple, set, and dictionary are True, except empty ones
there are not many values that evaluate to False, except empty values, such as (), [], {}, "", the
number 0, and the value None. And of course the value False evaluates to False
TYPE CASTING
Type Casting is the method to convert the Python variable datatype into a certain data type in order
to perform the required operation by users. In this, we will see the various techniques for
typecasting. There can be two types of Type Casting in Python:
In this, method, Python converts the datatype into another datatype automatically. Users
don’t have to involve in this process.
In this method, Python needs user involvement to convert the variable data type into the
required data type.
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
STRINGS
Strings in python are surrounded by either single quotation marks, or double quotation marks.
print("Hello")
print('Hello')
You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
print("It's alright")
print("He is called 'Johnny'")
print('He is called "Johnny"')
a = "Hello, World!"
print(len(a))
Slicing
Specify the start index and the end index, separated by a colon, to return a part of the string.
b = "Hello, World!"
print(b[2:5])
By leaving out the start index, the range will start at the first character
b = "Hello, World!"
print(b[:5])
By leaving out the end index, the range will go to the end.
b = "Hello, World!"
print(b[2:])
NEGATIVE INDEXING
Use negative indexes to start the slice from the end of the string.
b = "Hello, World!"
print(b[-5:-2])
Modify strings
Python has a set of built-in methods that you can use on strings.
UPPER CASE
LOWER CASE
a = "Hello, World!"
print(a.lower())
STRIP
The strip() method removes any whitespace from the beginning or the end:
REPLACE
a = "Hello, World!"
print(a.replace("H", "J"))
SPLIT
The split() method splits the string into substrings if it finds instances of the separator:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
CONCATENATE
a = "Hello"
b = "World"
c=a+b
print(c)
a = "Hello"
b = "World"
c=a+""+b
print(c)
we can combine strings and numbers by using f-strings or the format() method.
To specify a string as an f-string, simply put an f in front of the string literal, and add curly
brackets {} as placeholders for variables and other operations.
age = 36
txt = f"My name is John, I am {age}"
print(txt)
STRING METHODS