You are on page 1of 18

Python for beginners notes

#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized


use or distribution prohibited
In [1]:
#First Program
print("This is sparta!!!")
This is sparta!!!
In [2]:
#Variables
var1="John"
print(var1)
John
In [3]:
var1="Sam"
print(var1)
Sam
In [4]:
var1="Matt"
print(var1)
Matt
In [2]:
#Data-Type
a=10
type(a)
Out[2]:
int
In [3]:
a=10.5
type(a)
Out[3]:
float
In [1]:
a="sparta"
type(a)
Out[1]:
str
In [5]:
a=True
type(a)
Out[5]:
bool
In [7]:
a=3+4j
type(a)
Out[7]:
complex
In [ ]:
#Arithmetic Operators
In [8]:
a=10
b=20
In [9]:
print(a+b)
30
In [10]:
print(a-b)
-10
In [11]:
print(a*b)
200
In [12]:
print(a/b)
0.5
In [4]:
#Relational Operators
In [5]:
a=10
b=20
In [6]:
a>b
Out[6]:
False
In [7]:
a<b
Out[7]:
True
In [8]:
a==b
Out[8]:
False
In [9]:
a!=b
Out[9]:
True
In [10]:
#Logical Operators
In [12]:
a=True
b=False
In [14]:
a&b
Out[14]:
False
In [15]:
b&a
Out[15]:
False
In [16]:
b&b
Out[16]:
False
In [17]:
a&a
Out[17]:
True
In [18]:
a|b
Out[18]:
True
In [19]:
b|a
Out[19]:
True
In [20]:
a|a
Out[20]:
True
In [21]:
b|b
Out[21]:
False
In [22]:
#Strings
In [23]:
my_string="My name is John"
In [24]:
my_string[0]
Out[24]:
'M'
In [5]:
my_string="My name is John"
In [6]:
my_string[-1]
Out[6]:
'n'
In [26]:
my_string[0:4]
Out[26]:
'My n'
In [28]:
len(my_string)
Out[28]:
15
In [30]:
my_string.lower()
Out[30]:
'my name is john'
In [31]:
my_string.upper()
Out[31]:
'MY NAME IS JOHN'
In [33]:
my_string.replace('y','a')
Out[33]:
'Ma name is John'
In [7]:
new_string = "hello hello world"
In [8]:
new_string.count("hello")
Out[8]:
2
In [13]:
s1 = 'This is sparta!!!'
s1.find('sparta')
Out[13]:
8
In [12]:
s1.find('b')
Out[12]:
1
In [15]:
fruit = 'I like apples, mangoes, bananas'
fruit.split(',')
Out[15]:
['I like apples', ' mangoes', ' bananas']
In [ ]:
#Tuples in Python
In [18]:
tup1=(1,"a",True,2,"b",False)
In [17]:
tup1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-17-afd04ff38ac4> in <module>
----> 1 tup1

NameError: name 'tup1' is not defined


In [44]:
tup1[0]
Out[44]:
1
In [45]:
tup1[-1]
Out[45]:
False
In [22]:
tup1=(1,"a",True,2,"b",False)
tup1[1:4]
Out[22]:
('a', True, 2)
In [46]:
tup1[1:4]
Out[46]:
('a', True, 2)
In [49]:
tup1[2]="hello"
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-49-2fc16622751e> in <module>
----> 1 tup1[2]="hello"

TypeError: 'tuple' object does not support item assignment


In [50]:
tup1[6]=3+4j
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-50-3b75c4b77e6a> in <module>
----> 1 tup1[6]=3+4j

TypeError: 'tuple' object does not support item assignment


In [52]:
min(tup1)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-52-ce68c930ff7f> in <module>
----> 1 min(tup1)

TypeError: '<' not supported between instances of 'str' and 'int'


In [24]:
tup1=(1,"a",True,2,"b",False)
len(tup1)
Out[24]:
6
In [2]:
tup1 = (1,"a",True)
tup2 = (4,5,6)
In [3]:
tup2+tup1
Out[3]:
(4, 5, 6, 1, 'a', True)
In [31]:
tup1 = ('sparta',300)
tup2 = (4,5,6)
tup1*3 + tup2
Out[31]:
('sparta', 300, 'sparta', 300, 'sparta', 300, 4, 5, 6)
In [32]:
tup1=(1,2,3,4,5)
min(tup1)
Out[32]:
1
In [33]:
tup1=(1,2,3,4,5)
max(tup1)
Out[33]:
5
In [34]:
cmp(tup1,tup2)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-34-e27d9faf1f7f> in <module>
----> 1 cmp(tup1,tup2)

NameError: name 'cmp' is not defined


In [53]:
#List in Python
In [ ]:

In [56]:
l1=[1,"a",2,"b",3,"c"]
In [58]:
l1=[1,"a",2,"b",3,"c"]
l1[1]
Out[58]:
'a'
In [59]:
l1=[1,"a",2,"b",3,"c"]
l1[2:5]
Out[59]:
[2, 'b', 3]
In [35]:
l1=[1,"a",2,"b",3,"c"]
l1[0]=100
l1
Out[35]:
[100, 'a', 2, 'b', 3, 'c']
In [36]:
l1=[1,"a",2,"b",3,"c"]
l1.append("Sparta")
l1
Out[36]:
[1, 'a', 2, 'b', 3, 'c', 'Sparta']
In [62]:
l1
Out[62]:
[100, 'a', 2, 'b', 3, 'c', True]
In [37]:
l1=[1,"a",2,"b",3,"c"]
l1.pop()
l1
Out[37]:
[1, 'a', 2, 'b', 3]
In [38]:
l1
Out[38]:
[1, 'a', 2, 'b', 3]
In [41]:
l1=[1,"a",2,"b",3,"c"]
l1.insert(1,"Sparta")
l1
Out[41]:
[1, 'Sparta', 'a', 2, 'b', 3, 'c']
In [43]:
l1 = ["mango","banana","guava","apple"]
l1.sort()
l1
Out[43]:
['apple', 'banana', 'guava', 'mango']
In [44]:
l1 = [1,2,3]
l2 = ["a","b","c"]
l1+l2
Out[44]:
[1, 2, 3, 'a', 'b', 'c']
In [45]:
l1 = [1,"a",True]
l1*3
Out[45]:
[1, 'a', True, 1, 'a', True, 1, 'a', True]
In [ ]:
#Dictionary in Python
In [68]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
In [1]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.keys()
Out[1]:
dict_keys(['Apple', 'Orange', 'Banana', 'Guava'])
In [70]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.values()
Out[70]:
dict_values([10, 20, 30, 40])
In [74]:
fruit["Apple"]
Out[74]:
10
In [2]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit["Mango"]=50
fruit
Out[2]:
{'Apple': 10, 'Orange': 20, 'Banana': 30, 'Guava': 40, 'Mango': 50}
In [3]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40,"Mango":50}
fruit["Apple"]=100
fruit
Out[3]:
{'Apple': 100, 'Orange': 20, 'Banana': 30, 'Guava': 40, 'Mango': 50}
In [4]:
fruit1={"Apple":10,"Orange":20}
fruit2={"Banana":30,"Guava":40}

fruit1.update(fruit2)

fruit1
Out[4]:
{'Apple': 10, 'Orange': 20, 'Banana': 30, 'Guava': 40}
In [6]:
fruit={"Apple":10,"Orange":20,"Banana":30,"Guava":40}
fruit.pop("Orange")
fruit
Out[6]:
{'Apple': 10, 'Banana': 30, 'Guava': 40}
In [78]:
#Set in Python
In [1]:
s1={1,"a",True,2,"b",False}
s1
Out[1]:
{1, 2, False, 'a', 'b'}
In [7]:
s1={1,"a",True,2,"b",False}
s1.add("Hello")
s1
Out[7]:
{1, 2, False, 'Hello', 'a', 'b'}
In [8]:
s1={1,"a",True,2,"b",False}
s1.update([10,20,30])
s1
Out[8]:
{1, 10, 2, 20, 30, False, 'a', 'b'}
In [9]:
s1={1,"a",True,2,"b",False}
s1.remove("b")
s1
Out[9]:
{1, 2, False, 'a'}
In [13]:
s1 = {1,2,3,4,5,6}
s2 = {5,6,7,8,9}

s1.intersection(s2)
Out[13]:
{5, 6}

#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized


use or distribution prohibited
In [ ]:
#simple for loop
In [1]:
fruits = ["apple","mango","banana"]
In [2]:
for i in fruits:
print(i)
apple
mango
banana
In [3]:
#nested for loop
In [4]:
color=["blue","green","yellow"]
item=["book","ball","chair"]
In [5]:
for i in color:
for j in item:
print(i,j)
blue book
blue ball
blue chair
green book
green ball
green chair
yellow book
yellow ball
yellow chair
In [ ]:

functi#Proprietary content. ©Great Learning. All Rights Reserved.


Unauthorized use or distribution prohibited
In [1]:
def hello():
print("Hello World")
In [2]:
hello()
Hello World
In [3]:
#Function with parameter
In [4]:
def add10(x):
return x+10
In [14]:
add10(10)
Out[14]:
20
In [15]:
def even_odd(x):
if x%2==0:
print(x, " is even")
else:
print(x, " is odd")
In [16]:
even_odd(5)
5 is odd
In [1]:
#lambda functions
In [2]:
g = lambda x: x*x*x
print(g(7))
343
In [3]:
#lambda functions with filter
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x%2 != 0) , li))
print(final_list)
[5, 7, 97, 77, 23, 73, 61]
In [4]:
#lambda functions with map
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2 , li))
print(final_list)
[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]
In [5]:
from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print (sum)
193
In [ ]:

If statements

#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized


use or distribution prohibited
In [1]:
a=10
b=20
In [2]:
if b>a:
print("B is greater than A")
B is greater than A
In [3]:
if a>b:
print("A is greater than B")
In [4]:
if a>b:
print("A is greater than B")
else:
print("B is greater than A")
B is greater than A
In [5]:
#elif
In [6]:
a=10
b=20
c=30
In [8]:
if (a>b) & (a>c):
print("A is the greatest")
elif (b>a) & (b>c):
print("B is the greatest")
else:
print("C is the greatest")
C is the greatest
In [ ]:
#Tuple with if
In [15]:
tup1=("a","b","c")
In [16]:
if "a" in tup1:
print("a is present in tup1")
a is present in tup1
In [ ]:
#List with if
In [17]:
l1=["a","b","c"]
In [18]:
if l1[0]=="a":
l1[0]=100
In [19]:
l1
Out[19]:
[100, 'b', 'c']
In [20]:
#Dictionary with if
In [21]:
d1={"k1":10,"k2":20,"k3":30}
In [22]:
if d1["k1"]==10:
d1["k1"]=d1["k1"]+100
In [23]:
d1
Out[23]:
{'k1': 110, 'k2': 20, 'k3': 30}
In [ ]:

While loop

#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized


use or distribution prohibited
In [ ]:
#Printing 1-10 using while loop
In [2]:
i=1
while i<=10:
print(i)
i=i+1
1
2
3
4
5
6
7
8
9
10
In [ ]:
#Printing 2-table with while loop
In [3]:
i=1
n=2
while i<=10:
print(n," * ", i, " = ",n*i)
i=i+1
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
In [ ]:
#While with list
In [5]:
l1=[1,2,3,4,5]
In [12]:
i=0
while i<len(l1):
l1[i]=l1[i]+100
i=i+1
In [13]:
l1
Out[13]:
[101, 102, 103, 104, 105]
In [ ]:

Python basic problems

#Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized


use or distribution prohibited
In [ ]:
#check even odd
In [ ]:
num = int(input("Enter a number: "))
if (num % 2) == 0:
print(num, " is even")
else:
print(num, " is odd")
In [ ]:
#check positive, negative or 0
In [ ]:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
In [ ]:
#Factorial of a number
In [ ]:
factorial = 1
# check if the number is negative, positive or zero
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
factorial = factorial*i
print("The factorial of",num,"is",factorial)
In [ ]:
#Reversing a number
In [ ]:
n=int(input("Enter number: "))
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
print("Reverse of the number:",rev)
In [ ]:
#Check if it is a palindrome
In [ ]:
n=int(input("Enter number:"))
temp=n
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10
if(temp==rev):
print("The number is a palindrome!")
else:
print("The number isn't a palindrome!")
In [ ]:
#fibonacci - 0 1 1 2 3 5 8.......
In [16]:
n=int(input("Enter number:"))
a = 0
b = 1
if n < 0:
print("Incorrect input")
elif n == 0:
print(a)
elif n == 1:
print(a)
else:
for i in range(2,n):
c = a + b
a = b
b = c
print(b)
Enter number:3
1
In [2]:
# 10 is the total number to print
for num in range(6):
for i in range(num):
print (num,end=" ") #print number
# new line after each row to display pattern correctly
print("\n")

2 2

3 3 3

4 4 4 4

5 5 5 5 5
In [ ]:

You might also like