Ads

LightBlog

Latest

Saturday, April 17, 2021

BASIC PYTHON PROGRAM

  BASIC PYTHON PROGRAM

#DAY 1 !
#python programming

#hello world program

print("hello himanshu shekhar");
#variable intilization
x=2+3
print(x)
a=5-3
print(a)
b=5**2
print(b)
c=55/5
print(c)
d=55//5
print(d)
e="hello himanshu shekhar"
print(e)
f='hello'
print(f)
h='himnshu'+'shekhar'
print(h)
g=5*"hello"+"himanshu"+"shekhar"
print(g)
#knowing datatype
print(type(g))
print(type(b))
print(type(a))
m=3.2565
print(type(m))
n="True"
print(type(n))
p=a+b
print(p)
#printing in next line
print("himanshu \n shekhar")
#finding the address of variable
print(id(n))
print(id(f))
print(id(p))
aa=10
bb=20
cc=aa
dd=cc+bb
print(id(cc))
print(id(aa))
print(id(bb))
print(dd)
#printing the single location of chracter(it is always started with 0)
i='himanshu'
print(i)
j=i+'shekhar'
print(j)
print(i[5])
print(j[3])
print(i[-2])
print(j[0:5])
print("\n")
# DAY 2

# LIST in python

# In case of list we can have multiple type of value
a = ['himanshu', 'shekhar', 10, 20.5];
print(a)
print(a[1])
# out of range print(a[4])
print(a[2:3])
print(a[-1])
b = [30, 16, 'hello', 'this', 'is', 'monu'];
# merging two list
print(a + b)
print(a, b)
c = [a, b]
print(c)
# function
a.insert(3, 'jee');
print(a)
a.remove('shekhar')
print(a)
b.pop(2) # it remove the index value
print(b)
b.pop() # it delete last element from the list
print(b)
a.append('hello kon') # add the element at the end of the list
print(a)
del a[1:3] # use to delete the elements
print(a)
b.extend([50, 355, 68])
print(b)
c = [10, 30, 50, 60, 23, 100]

# multiple string line here
aaaa = '''hello this multiple,
string line and,
here we use 3 things'''
print(aaaa)

a = "hello WORLD"
print(a[1])
print(a[2:5])
print(a[-5:-2])
print(len(a))
print(a.strip()) # strip is use to remove whitespace from beginning or end
print(a.lower()) # convert string into lower case
print(a.upper()) # conert string into upper case
hs = "Himanshu Shekhar"
print(hs)
print(hs.replace("Himanshu", "Himi")) # it(replace) use both either a single character or word
ex = 4 ** 0.9
print(ex)
tupli = ("apple", "mango", "banana", "orange")
print(tupli)
# access tuple items
print(tupli[1:4])
print(tupli[1:-1])
print(tupli[-1]) # here -1 included
print(tupli[-3:-1])

xx = ("apple" ,"bannan" , "cherry")
y = list(xx)
y[1] = "grapes"
xx = tuple(y)
print(xx)

# Break statement: a break statement ebnables a program to skip over part of code ...Rule: A break statement terminates the very loops its lies within..




DAY 3

a = 35
b = 568
if b > a:
print("b is grater than a")

# Indentation (whitespace at the beginning of a line)

aa = 567
bb = 89
if aa>bb :
print("a is greater than b ")

# Elif

a=95
b=95
if a > b:
print("a is greater than b")
elif a == b:
print("both variables are equals")

# Else
a = 300
b =576
if a>b :
print("a is greater than b")
elif a==b:
print("both are equals")
else:
print("b is greater than a")

x = 3546
y = 657
if x<y:
print("y is greater than x")
else:
print("y is not greater than x")

#printing in same line
a = 789
b = 675
if a>b: print("a is greater than b")

# using the both if And else but execute 1
a = 456
b = 54798
print(" a is greater ") \
if a>b \
else \
print(" b is greater ")

#........................three statement in 3 line
a = 600
b = 600
print("a") if a>b else print("a is greater") if a==b else print(" both are equals")
# And keyword are use
a=60
b=567
c=679
if a>b or c>a:
print("atleast one condition are true")
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")

# nested if .........
x=601

if x > 41:
print(" above 41 is less")
if x>20:
print("this is also true")
else:
print("both statement are false")

#pass
a = 574
b = 67
if a>b:
pass


















 Loops in Python

Example

# While loop study in python


i = 3
while i < 10:
print(i)
i=i+1

# using the break statement in while loop
print(" this is the result of break statement")
i = 1
while i<10:
print(i)
if i == 5:
break
i+=1

# using the continue statement in while loop
print("this is the continue statement in while loop...continue (skip)")
i = 10
while i <30:
i +=1
if i == 20:
continue
print(i,"this is continue")
# using if else in while loops
i = 1
while i<10:
print(i)
i+=1
else:
print("i is no longer than 10")


# for loop exmaple in python:

iteams = ["mango","apple","pineapple"]
for x in iteams:
print(x)


for x in "mango":
print(x) # printing in sequence of chracter


# using break statement in for loop
#example 1
company = ["google","microsoft","bing"]
for x in company:
if x=="microsoft":
break
print(x)
print("second example below of break")
#example 2

brand = ["bmw","tata","mahindra","audi"]
for x in brand:
print(x)
if x=="mahindra":
break

# using else in for loop

for x in range(8):
print(x)
else:
print("finally finishing the code")

# nested for loop
company = ["mangal","himgange","navratan","patanjali"]
product = ["dalmot","hair oil","powder & oil","many iteams"]
for x in company:
for y in product:

print(x,y)

# using pass statement
# example 1
y= range(5)
for y in x:
pass
# example 2
for x in [1,2,3,4,5,6,7,8]:
pass



No comments:

Post a Comment