0% found this document useful (0 votes)
0 views6 pages

Data Types and Typecasting in Python (DAY-4)

Uploaded by

sree priya palle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Data Types and Typecasting in Python (DAY-4)

Uploaded by

sree priya palle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DATA TYPES IN PYTHON

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.

NUMERIC DATA TYPES:

Integer (int)

Represents whole numbers without a decimal point.

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))

x = float(1) # x will be 1.0


y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2

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)

In programming you often need to know if an expression is True or False.

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(type(x)) #display the data type of x

print(10 > 9)
print(10 == 9)
print(10 < 9)

Almost any value is evaluated to True if it has some sort of content.

Any string is True, except empty strings.

Any number is True, except 0.

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:

• Python Implicit Type Conversion

In this, method, Python converts the datatype into another datatype automatically. Users
don’t have to involve in this process.

• Python Explicit Type Conversion

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

#convert from int to float:


a = float(x)

#convert from float to int:


b = int(y)

#convert from int to complex:


c = complex(x)

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.

'hello' is the same as "hello".

You can display a string literal with the print() function.

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"')

To get the length of a string, use the len() function.

The len() function returns the length of a string.

a = "Hello, World!"
print(len(a))

Slicing

You can return a range of characters by using the slice syntax.

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])

The first character has index 0.

Slicing from the start

By leaving out the start index, the range will start at the first character

b = "Hello, World!"
print(b[:5])

Slicing from the end

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])

Get the characters:

From: "o" in "World!" (position -5)

To, but not included: "d" in "World!" (position -2)

Modify strings

Python has a set of built-in methods that you can use on strings.

UPPER CASE

The upper() method returns the string in upper case:


a = "Hello, World!"
print(a.upper())

LOWER CASE

The lower() method returns the string in lower case:

a = "Hello, World!"
print(a.lower())

STRIP

The strip() method removes any whitespace from the beginning or the end:

a = " Hello, World! "


print(a.strip()) # returns "Hello, World!"

REPLACE

The replace() method replaces a string with another string:

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

To concatenate, or combine, two strings you can use the + operator.

a = "Hello"
b = "World"
c=a+b
print(c)

To add a space between them, add a " ":

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

You might also like