Data Types in Python

We can have different types in python . Words are called characters and numbers can be Integer or floats.

Integers

Integers can be negative or Positive

Floats

Floats are real numbers which includes the integers but also "numbers in-between the integers"

Boolean

Boolean types takes true or False

In [69]:
# Data types in Python 
print(type(12)) # Int type

print(type(12.5)) #  Float Type

print(type("Hello")) # Type String

print(type(True)) # Type Boolean
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>

Type Conversion

Process of converting the type of one expression into another is known as casting In Python we can change the type of the expression in Python

  • convert or cast the integer float

If we cast a Boolean true to an integer or float we will get a 1, if we cast a Boolean false to an integer or float. If we get a zero if you cast a 1 to a boolean, you get a true similarly, if you cast a 0 to a Boolean you get a false.

In [12]:
# Cast float to Integer
print(int(1.1))

# convert string to Integer
print(int('1')) #We can do this as 1 is a integer type

# Cannot do this as "A" cannot be cast to Integer
# print(int("A")) 

# Convert int to String
str(1)

# Convert Float to String
str(1.32)

bool(1) # Casting 1 to boolean

print(bool(0)) # Casting  to boolean

print(float(True)) # Casting boolean to float

print(int(False))
1
1
False
1.0
0

Operations in Python

  • addition: 2 + 2
  • subtraction: 5 - 2
  • multiplication: 3 * 2
  • division: 4 / 2
  • exponentiation: 4 ** 2
  • division with rounded result : 5//2
In [14]:
## Divison with Rounded Results
print(5//2)

print(5/2) # Regular Division
2
2.5

Variables in Python

Variables are used in Python to store values for repeated use.

Here are some more characteristics of variables in Python:

  • variables store the output of a block of code
  • variables are typically assigned using = (as in x = 1)
In [ ]:
newVariable = 3 *3
print(newVariable)

Strings

String in python are Immutable ordered sequence.So once we assig a String ,we cannot change its values Each element in the sequence can be accessed using an index represented by the array of numbers

In [25]:
#Strings in Python 
cName="Michael Jackson"
print("First Index "+cName[0])
print("Third Index "+cName[6])

# Find the Length of String
lengthString = len(cName)
print("Length of String  "+str(lengthString))

print("13th Index "+cName[13])

#Using Negative Index to find String Values
print("First Value "+cName[-15])
print("Last Value " +cName[-1])


#Slicing Strings 

print("First four Characters "+cName[0:4])
print("Last Four Characters "+cName[8:12])


# Select every second variables from String
print(" Every second Variable in String "+cName[::2])

# Concatenate the String 
concatString = cName + " is the best"
print("Concatenated String "+concatString)


# Creating Tuples from String 

newTuple = cName * 3 # Creates a Tuple from cName by repeatimg 3 Times 
print(newTuple)

xy ='023456789'
print(xy[::2]) # Prints every 2 characters 
First Index M
Third Index l
Length of String  15
13th Index o
First Value M
Last Value n
First four Characters Mich
Last Four Characters Jack
 Every second Variable in String McalJcsn
Concatenated String Michael Jackson is the best
Michael JacksonMichael JacksonMichael Jackson
03579

Escape Sequences

  • \ are meant to proceed escape sequences
  • escape sequences are string that are difficult to input
  • \n represents a new line
  • \t represents a new tab
  • To place a blackslash() in the string use \
In [65]:
# Example of Escape Sequences which splits string with new line escape sequences
print("Michael Jackson \n is th best")

# Add Back Slash to the String
print("Michael Jackson \\ is the best")

# New line Escape Sequence example
print("Python \n is Best")

# Adding Back Slash in Python
print("Python \\ is Best ")
Michael Jackson 
 is th best
Michael Jackson \ is the best
 MICHAEL JACKSON IS THE BEST 
 Janet Jackson is the best 
-1
6

String methods

  • upper() converts string to upper case
  • replace() replaces the method
  • find(search parameters) is used to find specific sub string
In [16]:
# Use of Upper in Python
A =" Michael Jackson is the best "
B = A.upper()
print(B)

# replace method
C= A.replace("Michael","Janet")
print(C)

#Using Find method to search for String 
print(A.find("Janet"))# Since Janet is not in A ,it will give -1
print(A.find("el")) # It is able to find "el" in the given String

fullName = "Nitendra Gautam"

print(fullName)



#Upper Case
upperCaseName = fullName.upper()
print(upperCaseName)

#LowerCase
print(" Lower Case "+fullName.lower())

# Finding Subs String 
myName = "Nitendra Gautam"
print(myName.find('Nitendra Gautam'))
 MICHAEL JACKSON IS THE BEST 
 Janet Jackson is the best 
-1
6
Nitendra Gautam
NITENDRA GAUTAM
 Lower Case nitendra gautam
0