We can have different types in python . Words are called characters and numbers can be Integer or floats.
Integers can be negative or Positive
Floats are real numbers which includes the integers but also "numbers in-between the integers"
Boolean types takes true or False
# 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
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
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.
# 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))
## Divison with Rounded Results
print(5//2)
print(5/2) # Regular Division
Variables are used in Python to store values for repeated use.
Here are some more characteristics of variables in Python:
newVariable = 3 *3
print(newVariable)
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
#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
# 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 ")
# 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'))