INT
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
FLOAT
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
INT
x = 1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
COMPLEX
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
TYPE CONVERSION
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))
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
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
simple interst:
p = int(input("Enter the principal amount :"))
t = int(input("Enter the time period :"))
r = int(input("Enter the rate of interest :"))
print('The principal is', p)
print('The time period is', t)
print('The rate of interest is',r)
si = (p * t * r)/100
print('The Simple Interest is', si)
fib;
n=int(input("enter the fib value"))
a=0
b=1
print(a)
print(b)
for i in range(2, n):
c=a+b
print(c)
a=b
b=c
Factorial:
n=int(input("enter the factorial value"))
f=1
for i in range(1, n+1):
f=f*i
print(f)
Type conversion between Tuple and
List
t = (1, 2, 3, 4)
l = [5, 6, 7, 8]
# Converting to tuple
T = tuple(l)
print(T)
print(type(T))
# Converting to list
L = list(t)
print(L)
print(type(L))
List
thislist = ["orange", "mango",
"kiwi", "pineapple", "banana"]
[Link]()
print(thislist)
thislist = [100, 50, 65, 82, 23]
[Link]()
print(thislist)
sort in descending
thislist =
["orange", "mango", "kiwi", "pineapple", "
banana"]
[Link](reverse = True)
print(thislist)
thislist = [100, 50, 65, 82, 23]
[Link](reverse = True)
print(thislist)
copy list
thislist = ["apple", "banana",
"cherry"]
mylist = [Link]()
print(mylist)
Join list
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
Tuple
tuple1 = ("apple", "banana",
"cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
print(tuple1)
print(tuple2)
print(tuple3)
Access tuple
thistuple =
("apple", "banana", "cherry")
print(thistuple[1])
update tuple
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Range
Python which returns a sequence of numbers starting from 0 and increments to 1
until it reaches a specified number.
We use range() function with for and while loop to generate a sequence of numbers.
Following is the syntax of the function:
range(start, stop, step)
for i in range(5):
print(i)
for i in range(1, 5):
print(i)
for i in range(1, 5, 2):
print(i)
Dictionary
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
STRING
a = "Hello, World!"
print(a[1])
for x in "banana":
print(x)
a = "Hello, World!"
print(len(a))
String Data Type
str = 'Hello World!'
print (str) # Prints complete string
print (str[0])# Prints first character of the string
print (str[2:5])# Prints characters
starting from 3rd to 5th
print (str[2:])# Prints string
starting from 3rd character
print (str * 2)# Prints string two
times
print (str + "TEST") # Prints
concatenated string
# List slicing in Python
my_list = ['p','r','o','g','r','a','m','i','z']
# items from index 2 to index 4
print(my_list[2:5])
# items from index 5 to end
print(my_list[5:])
# items beginning to end
print(my_list[:])
print(my_list[:3])