You are on page 1of 9

**BASICS OF PYTHON--DAY 1**  

                               

ABHISHEK MANIKANDAN 40110017-2ND YEAR, A1 SECTION

**BUILT IN DATA TYPES:**

In [2]:

##int
a=5
print(a)
print(type(a))

##string
b="5"
print(b)
print(type(b))

##float
c=5.5
print(c)
print(type(c))

<class 'int'>

<class 'str'>

5.5

<class 'float'>

# This is formatted as code

CONDITIONAL:

In [3]:

print(a<c)
print(c<a)

True

False

In [4]:

q=20
if(c<q):
q=q+1
print(q)

21

USER DEFINED:
In [5]:

list=['qwerty',3,6,'asdf']

tuple=('ufo','aliens','mars')

dict={'phone':'52176312','name':'abhishek'}

set={'asdnas','ASNDMSD','BROWNCOW'}

FOR LOOP WITH DIFFERENT RANGES:

In [6]:

for i in range(4):
print(list[i])

qwerty

asdf

In [7]:

for x in range(0,4,1):
print(list[i])

element=list[0]
print(element)

asdf

asdf

asdf

asdf

qwerty

In [8]:

##getting value from key="name"


W=dict.get("name")
##getting all the keys from the dict
b=dict.keys()

print(W)
print(b)

abhishek

dict_keys(['phone', 'name'])

In [9]:

##printing set elements


print(set)

{'asdnas', 'ASNDMSD', 'BROWNCOW'}

WHILE LOOP:
In [10]:

while(a<c):
a=a+1
print(c)

5.5

SLICING:

In [11]:

print(list[:2])

['qwerty', 3]

APPEND AND INSERT:

In [13]:

list.append(67)
print(list)

['qwerty', 3, 6, 'asdf', 67, 67]

In [15]:

list.insert(3,"puppy")
print(list)

['qwerty', 3, 6, 'puppy', 'asdf', 67, 67]

TAKING LIST INPUT:

In [17]:

arr=[]
n=int(input("number of digits?"))
for i in range(n):
inputer=int(input())
arr.append(inputer)

print(arr)

number of digits?3

[4, 6, 7]

PROBLEMS

1.FACTORIAL:
In [25]:

num = int(input("Enter a number: "))


factorial = 1
if num < 0:
print(" 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 is",factorial)

Enter a number: 3

The factorial of is 6

2.PRIME NUMBER:

In [26]:

num = 29

num = int(input("Enter a number: "))

flag = False

if num > 1:

for i in range(2, num):


if (num % i) == 0:
flag = True
break
if flag:
print(num, "is not a prime number")
else:
print(num, "is a prime number")

Enter a number: 7

7 is a prime number

3.PALINDROME:
In [32]:

def isPalindrome(s):
return s == s[::-1]

s = "racecar"
ans = isPalindrome(s)

if ans:

print("Yes")
else:

print("No")

Yes

4.ARMSTRONG NUMBER:
In [28]:

num = int(input("Enter a number: "))

sum = 0

temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10

if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")

def power(x, y):

if y == 0:
return 1
if y % 2 == 0:
return power(x, y // 2) * power(x, y // 2)

return x * power(x, y // 2) * power(x, y // 2)

def order(x):

n = 0
while (x != 0):
n = n + 1
x = x // 10

return n

def isArmstrong(x):

n = order(x)
temp = x
sum1 = 0

while (temp != 0):


r = temp % 10
sum1 = sum1 + power(r, n)
temp = temp // 10

return (sum1 == x)

x = 153
print(isArmstrong(x))

x = 1253
print(isArmstrong(x))

Enter a number: 456

456 is not an Armstrong number

5.SUM OF FIRST AND LAST DIGITS OF THE NUMBER

In [34]:

def firstDigit(n) :

while n >= 10:


n = n / 10
return int(n)

def lastDigit(n) :

return (n % 10)

n = 7687385

first=firstDigit(n)
last=lastDigit(n)

print(first+last)

12

6.COMPUTE NPR:

In [31]:

import math
def fact(n):
if (n <= 1):
return 1

return n * fact(n - 1)

def nPr(n, r):

return math.floor(fact(n) /
fact(n - r))

n = 10
r = 7

print(n, "P", r, "=", nPr(n, r))

10 P 7 = 604800

7.NUMBER OF DIGITS:

In [24]:

a=str(3297457123649)
print(len(a))

13

NUMPY:

In [ ]:

In [35]:

import numpy as np

ARITHMETIC OPERATION:

In [40]:

arr1=np.array([1,6,3,8,3,2])
print(arr)

arr2=np.array([4,8,6,1,2,5])
print(np.multiply(arr1,arr2))

[1 6 3 8 3 2]

[ 4 48 18 8 6 10]

In [54]:

comb=np.array([arr1,arr2])

In [39]:

arr1[0:4]

Out[39]:

array([1, 6, 3, 8])

SHAPE AND SIZE

In [56]:

arr3=np.array([[4,7,9,0],[2,7,1,6],[2,8,3,2]])
print(arr3.shape)
print(arr.size)

(3, 4)

VSTACK:

In [53]:

np.vstack((arr1,arr2))

Out[53]:

array([[1, 6, 3, 8, 3, 2],

[4, 8, 6, 1, 2, 5]])

WHERE CONDITION:

In [55]:

comb=np.array([arr1,arr2])
np.where(comb<3,comb,0)

Out[55]:

array([[1, 0, 0, 0, 0, 2],

[0, 0, 0, 1, 2, 0]])

SLICE:

In [59]:

print(comb[0:4])

[[1 6 3 8 3 2]

[4 8 6 1 2 5]]

RESHAPE:

In [60]:

recomb=comb.reshape([3,4])

In [61]:

print(recomb)

[[1 6 3 8]

[3 2 4 8]

[6 1 2 5]]

You might also like