Skip to content

Commit be5f342

Browse files
committed
basic pythone programs
1 parent e8e2dac commit be5f342

16 files changed

+430
-1
lines changed

.vscode/settings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"python.testing.pytestArgs": [
3+
"."
4+
],
5+
"python.testing.unittestEnabled": false,
6+
"python.testing.nosetestsEnabled": false,
7+
"python.testing.pytestEnabled": true
8+
}

__pycache__/module.cpython-38.pyc

244 Bytes
Binary file not shown.

datatype.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
demolist = [1,2,3,4,5,6,7]
2+
# demolist.remove(1)
3+
print(demolist.count(2))
4+
print(demolist[:])
5+
userdetail = [
6+
'name','chaudhary suhsil',
7+
'email','sushil@gmail.com'
8+
]
9+
userdetail[0]='sushil'
10+
userdetail.extend([1,2,3])
11+
# print(count(userdetail))
12+
print(userdetail)
13+
14+
# pworser in case of lsit comparhension
15+
pow2 = [2 ** x for x in range(10)]
16+
print(pow2)
17+
18+
# comperhansion list is awesome
19+
newpow = [2 ** x for x in range(10) if(x > 5)]
20+
print(newpow)
21+
22+
# tuple
23+
# this is called tuple packing
24+
newtup = 3,4.6,'dog'
25+
print(newtup)
26+
# this tuple unpacking
27+
a, b, c = newtup
28+
print(a)
29+
print(b)
30+
print(c)
31+
32+
# string
33+
count = 0
34+
for letter in 'hello sushil':
35+
if letter == 'l':
36+
count += 1
37+
print(count,'letter found')
38+
myname = 'sushil'
39+
myname_enumarate = list(enumerate(myname))
40+
print(myname_enumarate)
41+
print(len(myname))
42+
print('hello, "kyas haal hai"')
43+
44+
# set are mutable and in between set also use all datatypes which are mutable
45+
print(type({1,2,3,4,'hello'}))
46+
print({1,2,3,4})
47+
myset = {'hello','hi','how','areyou'}
48+
print(myset)
49+
myset.add('sushil')
50+
myset.update(['suraj','sunil'])
51+
myset.discard('hi')
52+
myset.remove('how')
53+
print(myset)
54+
# set use for performing mathematical operation
55+
aset = {1,2,3,4,5}
56+
bset = {5,6,7,8}
57+
print('set union: ',aset|bset)
58+
print('using unio function: ',aset.union(bset))
59+
print('intersion:',aset & bset)
60+
print('using itersetion fun:',aset.intersection(bset))
61+
print('difference:',aset-bset)
62+
print('deferencs fun:',aset.difference(bset))
63+
print('Sementric difference:',aset^bset)
64+
print('semenmtric fun:',aset.symmetric_difference(bset))
65+
print('print in iteration using for:')
66+
for letter in aset:
67+
print('for loop:',letter)
68+
print('frozen set')
69+
afroz = frozenset([1,2,3,4,5])
70+
bfroz = frozenset([6,7,8,9])
71+
print('disjoint bothe:',afroz.isdisjoint(bfroz))
72+
print('diffrence in frozen:',afroz.difference(bfroz))
73+
74+
# dictonary
75+
print('use of dictornay is same as set but its use key value and use curly backet')
76+
adict = {'name': 'sushil','email':'sushil@gmail.com'}
77+
adict['name'] = 'chaudhary sushil'
78+
adict['mobile'] = '89899889'
79+
print(adict.get('name'))
80+
print(adict)
81+
# print(adict.popitem())
82+
odddict = {x: x*x for x in range(6) if x % 2 == 1}
83+
print({x: x*x for x in range(6) if x%2==1})
84+
print(odddict)
85+
for i in odddict:
86+
print('In interation:',odddict[i])
87+
88+

demo.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# print('Hello')
2+
# a=3
3+
# if a <= 3:
4+
# print('Value of a:',a)
5+
# print('Hello next print')
6+
7+
# # variables
8+
# a = 'hello'
9+
# a,b,c = 1,2,3
10+
# print(a)
11+
# print(b)
12+
# print(c)
13+
# print(type(a))
14+
15+
# name = 'python'
16+
# # print('Hello '+name)
17+
18+
# # global var
19+
# funname = 'hello python' #this is a global variable
20+
21+
# def demofunction():
22+
# print(name)
23+
24+
# demofunction()
25+
# # a->b
26+
# # array['sadfa','asdf','sdf']
27+
# # array[0]
28+
29+
# # data type conversiton
30+
31+
# a = '1'
32+
# b = 1.0
33+
# print(a)
34+
# print(type(b))
35+
# print(type(int(b)))
36+
37+
import random
38+
39+
print(random.randrange(2000,2005))
40+
41+
string = '''hello
42+
hi
43+
how are you'''
44+
astirng = ' hello python '
45+
alist = ['hello','hi','how','are','you']
46+
print(alist[-1])
47+
print(astirng[-9])
48+
print(astirng.islower())
49+
print(astirng.replace('hello','hi'))
50+
print(len(astirng))
51+
num = 46
52+
print('hell' in astirng)
53+
print('Hello number+',num)
54+
print('hello {0}, welcome to {1}'.format('Vanshika','Python'))
55+
print("Hello \"New\" Python")
56+
print(astirng.count('h'))
57+
# print(type(astirng.translate))
58+
59+
avalue = 5
60+
b = 6
61+
# avalue += 7
62+
print('b value:',b)
63+
print('SUm of avalue is:',avalue)
64+
print(astirng.islower())
65+
if (not(avalue == 6)):
66+
print('Get bothe value')
67+
listval = [1, 2, 3, 4]
68+
print(next(iter(int,listval)))
69+
# print(next(iter(listval)))
70+
71+
# for num in [1,2,3,4]:
72+
# print(num)

exception.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# sys use to get exceptions
2+
import sys
3+
4+
rendomList = ['a',0,2]
5+
6+
for num in rendomList:
7+
try:
8+
print('Value print is: ',num)
9+
r = 1/int(num)
10+
break
11+
except:
12+
print('Oppss sys problem: ',sys.exc_info()[0],'occured')
13+
print('Next entry')
14+
print('The receprocal of',num,'is',r)
15+
16+
17+
# create class
18+
# parent class
19+
class Birds:
20+
21+
def __init__(self):
22+
print('Bird is ready')
23+
24+
def whoisThis(self):
25+
print('Bird')
26+
def swim(self):
27+
print('Swim faster')
28+
29+
# child class inherit parent calss Birds
30+
class Piggen(Birds):
31+
32+
def __init__(self):
33+
# call super class
34+
super.__init__()
35+
print('Pegun is ready')
36+
37+
def whoisThis(self):
38+
print('Pegun is ready')
39+
40+
41+
42+
print(Birds().whoisThis())

fdirectory.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import os
2+
3+
os.getcwd()
4+
print(os.getcwdb())
5+
print(os.listdir())
6+
# print(check)
7+
# print(os.mkdir('testdir'))
8+
print(os.listdir())
9+
# os.mkdir('testdir')
10+
# create file in directory
11+
fname = open('testdir/text.txt', 'a')
12+
fwrite = open('testdir/text.txt','w')
13+
fwrite.write('Hello sushil how are you\nthis is firs directory inccer page')
14+
# read the file
15+
# fread = open('testdir/text.txt','r')
16+
print(open('testdir/text.txt', 'r').read())
17+
# print(os.remove('testdir/text.txt'))
18+
19+
# use shutil modul to delete non empty dierecoty
20+
import shutil
21+
shutil.rmtree('testdir')
22+
# fname.write = 'hello sushil\nthis is a dmeo file which i just created ok.'
23+
# print(fname.read())
24+
# print(os.rename('testdir','newtest'))
25+
# print(os.rmdir('newtest'))
26+
print(os.listdir())
27+
28+
def funcname():
29+
pass
30+
31+
# function functionName(){
32+
# function body
33+
# }
34+
a = '1'
35+
b = 1
36+
print(a)

file.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
print('File operations')
2+
# f = open('file.txt')
3+
# print('Is file open:',f)
4+
# newf = open('hello.txt', mode='a')
5+
# print('Create new file hello.txt:',newf)
6+
# f.close()
7+
# newf.close()
8+
9+
# use safe mode avop mode is not safe on so use tye filan
10+
try:
11+
f = open('file.txt')
12+
# best way to use the is - with
13+
14+
with open('file.txt',mode='w') as f:
15+
f.write('kya baat hai maza hi aa gaya. mast hai python hain na\n')
16+
f.write('ye jo bate hai ye to hoti hi rangei\n\n')
17+
f.write('magar agar kaha jaye to maja aata hai python me aur hai hi majedat')
18+
# f.write('this is my first file\n')
19+
# f.write('no problem to wirht here\n')
20+
# f.write('it\'s great idea to writh in the file by python.\n\n')
21+
# f.write('really awesome')
22+
# with open('file.txt','r') as f:
23+
# print(f)
24+
25+
# newf = open('hello.txt',mode='a')
26+
# print('created file:',f)
27+
# print('Create new file using a:',newf)
28+
finally:
29+
f.close()
30+
# newf.close()
31+
32+
# read('file.txt')
33+
readfile = open('file.txt','r')
34+
print(open('file.txt', 'r').read())
35+
print('current file position:',readfile.tell())
36+
print('bright currsor to initial posion',readfile.seek(0))
37+
38+
# read file in loop
39+
print('\n\n')
40+
for i in readfile:
41+
print(i,end='') #end use to stope reapeting double new line
42+
43+
print(readfile.readline())

file.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
kya baat hai maza hi aa gaya. mast hai python hain na
2+
ye jo bate hai ye to hoti hi rangei
3+
4+
magar agar kaha jaye to maja aata hai python me aur hai hi majedat

hello.txt

Whitespace-only changes.

ifelse.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# if else stament
2+
3+
# check negative or possitive case here
4+
num = float(input('Enter your number:'))
5+
if num >= 0:
6+
if num >= 1:
7+
print ('Num value is possitive')
8+
else:
9+
print('Value is zero')
10+
else:
11+
print ('Value is negative')

loop.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# loop in python using 2 paremeters
2+
# first is the variable which initialize before start
3+
# second is the sequesnce in which the loop rounding
4+
# like in py use a list,tuple,dictroney, string which provide many number of values
5+
6+
# --------------------------------------------------------------------
7+
# ----------------FINAL ABOUT LOOP----------------------
8+
# -----------------------------------------------------------------------
9+
# loop is using only 2 parametres on loop case in for loop firs is the initialization value wich you can direct initialize here no need to initialize before and next the the way of how the loop itirate the same for the time .
10+
# in this case on simple case on 2nd paremeter pass the list,dictonary,string value of repet the loop until not finish the value on these.
11+
# on other side if we want to repeate the loop for same type of number like print 0 to 100 number in loop. In this casea py provides a range() predefine function which you also use direct without use of loop in just print case
12+
# but on print case it use also if you want to use the sam ein case of loop then everything work same just change the second paremeter.
13+
# like for initialize-value in list-value:
14+
# in this case the list-value will use in range and in rage also use len-for get the length of the list to repect the loop untile get the end value
15+
# in this case the for loop lookes like
16+
# for i in range(len(itemsvalue))
17+
18+
# list of number
19+
listnum = ['hello','hai','how','are','you']
20+
# print(len(listnum))
21+
22+
for i in range(len(listnum)):
23+
print(listnum[i])
24+
25+
# print(list(range(0,10)))
26+
# print(list(range(0,100)))
27+
digit = [1,2,3]
28+
for hello in digit:
29+
print(hello)
30+
# continue
31+
else:
32+
print('No item left')
33+
34+
# ----------------------------------
35+
# WHILE LOOP
36+
n = 10
37+
sum = 0
38+
i=1
39+
while i <= n:
40+
sum = sum + i
41+
# print(len(n))
42+
i = i+1
43+
print('get',sum)
44+
45+
# whicle with else
46+
counter = 0
47+
while counter < 3:
48+
print('get counter')
49+
counter = counter+1
50+
else:
51+
print('stop counter')
52+
53+
for val in 'string':
54+
if val == 'i':
55+
continue
56+
print(val)
57+
# break
58+
else:
59+
print('finshed string')
60+
61+
# function
62+
def simplefun(name):
63+
return 'Hello, ' + name + ' how are you.'
64+
# return
65+
66+
print (simplefun('Roshan'))
67+
68+
# lambda function
69+
oldlist = [1,2,3,4,5,6,7,8,9]
70+
newlist = list(map(lambda x: x*2, oldlist))
71+
print('New list is: ',newlist)
72+
73+

module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def sumOfTwo(num1, num2):
2+
# print(num1 + ' and ' + num2 )
3+
result = num1 + num2
4+
return result

0 commit comments

Comments
 (0)