Skip to content

Commit 2c69388

Browse files
python basic
1 parent d99c15a commit 2c69388

12 files changed

+335
-0
lines changed

Calculator [customize user input].py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
def refomer(operator,x,y):
3+
x=str(x)
4+
y=str(y)
5+
6+
x=x.replace(" ","")
7+
y=y.replace(" ","")
8+
try:
9+
x = int(x)
10+
y = int(y)
11+
except:
12+
x = float(x)
13+
y = float(y)
14+
finally:
15+
value = calculator(str(operator),x,y)
16+
17+
return x,y,value;
18+
19+
def calculator(operator,x,y):
20+
return {
21+
'+': lambda : x+y,
22+
'-': lambda : x-y,
23+
'*': lambda : x*y,
24+
'/': lambda : x/y,
25+
}.get(operator,"Not Available")()
26+
27+
28+
strr=input("Enter the expresion <x+y>: ")
29+
op = ['+','-','*','/']
30+
count = 0
31+
for num in strr:
32+
if num in op:
33+
opee = num
34+
count+=1
35+
if count>1:
36+
break;
37+
38+
if count==1:
39+
try:
40+
x,y=strr.split(f'{opee}')
41+
except:
42+
print("Wrong Formated value: input <x+y> format")
43+
else:
44+
x,y,val= refomer(opee,x,y)
45+
print(f"{x} {opee} {y} = {val}")
46+

Capitalization.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Just simple capitalize
2+
name=["ashik"]
3+
Name=[name[0][0].upper()+name[0][1:]]
4+
Name=str(Name[0])
5+
print(Name)
6+
7+
#user inputed data capitalize
8+
user_inputed_name="ayemun hossain"
9+
name_list=[word[0].upper()+word[1:] for word in user_inputed_name.split()]
10+
name=" ".join(name_list)
11+
print(name)
12+
13+
#user inputed data capitalize : 2 <complex>
14+
user_inputed_name="AyEmun hosSaiN"
15+
name_list=[word[0].upper()+word[1:].lower() for word in user_inputed_name.split()]
16+
name=" ".join(name_list)
17+
print(name)

Demo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am Ashik. I read in Daffodil International University.

File Handeling part 1.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#;==========================================
2+
#; Title: File handeling part 1
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
7+
#Read a file :
8+
file = open("story.txt","r")
9+
containt=file.read()
10+
file.close()
11+
print(containt)
12+
13+
#read file line by line :
14+
file=open("story.txt","r")
15+
[print(i,end="") for i in file]
16+
file.close()
17+
18+
#Read specific amount of bit :
19+
file = open("story.txt","r")
20+
content=file.read(10)
21+
file.close()
22+
print(content)
23+
24+
#write a file :
25+
file=open("story2.txt","w")
26+
file.write("This story is about the boy who live.\nHis name is Harry Potter.\n")
27+
file.close()
28+
29+
#check out that file
30+
file = open("story2.txt","r")
31+
content=file.read()
32+
file.close()
33+
print(content)
34+
35+
#append a file
36+
file=open("story2.txt","a+")
37+
file.write("The boy was cursed by \"Voldemort\".\n")
38+
file.close()
39+
file = open("story2.txt","r")
40+
content=file.read()
41+
file.close()
42+
print(content)
43+
44+
#append a file and read:
45+
file=open("story2.txt","a+")
46+
file.write("\"Voldemort\" Killed Harry's parents.\n")
47+
file.seek(0)
48+
content=file.read()
49+
print(content)
50+
51+
#Print the actual file name :
52+
f=open("story2.txt",'r')
53+
print(f.name)
54+
f.close()

File Handeling part 2.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#;==========================================
2+
#; Title: File handeling part 2
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
7+
#Iterate the file line by line
8+
9+
f=open("story2.txt",'r')
10+
[print(i,end="") for i in f]
11+
f.close()
12+
13+
14+
#Read file and storing as list of lines
15+
file = open("story2.txt","r")
16+
17+
data_list = file.readlines()
18+
file.close()
19+
print(data_list)
20+
21+
#mode function in file :
22+
file = open("story2.txt","r")
23+
24+
if file.mode == 'r':
25+
print("File is open as read mode")
26+
elif file.mode == 'w':
27+
print("File is open as write mode")
28+
elif file.mode == 'a+':
29+
print("Fil is open as append mode")
30+
31+
32+
#try case on file opening
33+
34+
try :
35+
f=open("secret_password.txt", "r")
36+
except:
37+
print("File is not exist ")
38+
else :
39+
pass #further process will done here
40+
finally:
41+
print("__Done__")
42+
43+
string_list = ["I","am","Ashik.","I","read","in","Daffodil","International","University.",]
44+
45+
#Write a sequence to file :
46+
f = open("demo.txt","w")
47+
f.writelines(' '.join(i for i in string_list))
48+
f.close()
49+
50+
seq=['Hello\n','I am Ashik\n','I read in Daffodil International University\n','I live in Dhanmondi\n']
51+
f=open("info.txt",'w')
52+
f.writelines(seq)
53+
f.close()
54+
#check the file
55+
f=open("info.txt",'r')
56+
print(f.read())
57+
f.close()
58+
59+
#Rename the file name:
60+
import os
61+
os.rename("demo.txt","Demo.txt")
62+
63+
#Delete the file:
64+
os.remove("Demo.txt")

Switch Case design.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def switch(a):
2+
switcher={
3+
1:'Ashik',
4+
2:'Malak',
5+
3:'Ahiya',
6+
4:'Amina',
7+
5:'Rafik',
8+
6:'Habib'
9+
}
10+
return switcher.get(a,"Not Found")
11+
12+
13+
print(switch(1)) #on you condition call the function with value

SystemRandom part 1.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#;==========================================
2+
#; Title: System Random Module
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
import random as r
7+
#Cryptographically Secured Randmon Module
8+
9+
sr=r.SystemRandom()
10+
sr.seed()
11+
12+
#Now sr is the instance of SystemRandom And you can use any kind of methods
13+
#That's works in random module and will be cryptographically secure
14+
15+
16+
#Crypthographically Secure Random Float number:
17+
randomFoat=sr.uniform(10,15)
18+
print(round(randomFoat,5))
19+
20+
21+
#Crypthographically Secure Random ascci <lower and upper>
22+
import string
23+
word = string.ascii_letters
24+
secure_word = "".join(sr.choices(word,k=5))
25+
print(secure_word)
26+
27+
28+
#Crypthographically Secure Random ascci letter and numbers:
29+
w_n = string.ascii_letters+string.digits
30+
secure_w_n = "".join(sr.choices(w_n,k=10))
31+
print(secure_w_n)
32+
33+
#let's make uniqe that will be more secure:
34+
more_wn = string.ascii_letters+string.digits
35+
secure_more_wn = "".join(sr.sample(more_wn,k=25))
36+
print(secure_more_wn)
37+
38+
#Even more strong combinaton with :
39+
40+
mixed = string.ascii_letters+string.digits+string.punctuation
41+
secure_mixed = "".join(sr.sample(mixed,k=30))
42+
print(secure_mixed)

SystemRandom part 2.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#;==========================================
2+
#; Title: System Random Module part 2
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
6+
#Get more specific character if we want
7+
8+
import random as rn
9+
sr = rn.SystemRandom()
10+
import string
11+
sr.seed()
12+
13+
#Secure Random Lower Case Ascii Character
14+
lower_ascci = string.ascii_lowercase
15+
safe_lower = "".join(sr.sample(lower_ascci,k=5))
16+
print(safe_lower)
17+
18+
#Secure Random Upper Case Ascii Character
19+
upper = string.ascii_uppercase
20+
safe_upper = "".join(sr.sample(upper,k=5))
21+
print(safe_upper)
22+
23+
#Task : ONE
24+
#->Generate a ten-character password with
25+
#one lowercase character
26+
#one uppercase characteR
27+
#one digits
28+
#one special character.
29+
30+
rand_sor=string.ascii_letters+string.digits+string.punctuation
31+
passo=sr.choice(string.punctuation)
32+
passo+=sr.choice(string.ascii_letters)
33+
passo+=sr.choice(string.digits)
34+
passo+="".join(sr.sample(rand_sor,7))
35+
pass_list = list(passo)
36+
sr.shuffle(pass_list)
37+
print("".join(pass_list))

SystemRandom part 3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#;==========================================
2+
#; Title: System Random Module part 3
3+
#; Author: @AyemunHossain
4+
#;==========================================
5+
import string
6+
7+
#secrets modules to generate the cryptographically secured data
8+
import secrets
9+
sec_rand = secrets.SystemRandom()
10+
11+
flt = sec_rand.uniform(0,10)
12+
print(round(flt,3))
13+
14+
print(f"Random Number bellow 10 : {secrets.randbelow(10)}")
15+
16+
#Secure unsigned integer with k random bits.
17+
print(f"Secure unsigned integer {secrets.randbits(k=10)}")
18+
19+
20+
#Generate a secure string with secret modules :
21+
print(f"Secure string -{secrets.token_bytes(32)}")
22+
23+
print(f"Secure sting <hex format>: {secrets.token_hex(32)}")
24+
25+
26+
#Secret Choices:
27+
password = "".join(sec_rand.choices(string.ascii_letters+string.digits+string.digits,k=8))
28+
print(password)
29+
30+
31+
#Secure token for Passwords,OTP,session keys,Password changing,registration confirm,etc:
32+
print(f"URL : https://adobil.com/user/ashik001/reset={secrets.token_urlsafe(96)}")

info.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Hello
2+
I am Ashik
3+
I read in Daffodil International University
4+
I live in Dhanmondi

story.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
An old man lived in the village. He was one of the most unfortunate people in the world. The whole village
2+
was tired of him; he was always gloomy, he constantly complained and was always in a bad mood.
3+
4+
The longer he lived, the more bile he was becoming and the more poisonous were his words. People avoided him,
5+
because his misfortune became contagious. It was even unnatural and insulting to be happy next to him.
6+
7+
He created the feeling of unhappiness in others.
8+
9+
But one day, when he turned eighty years old, an incredible thing happened. Instantly everyone started hearing the rumour:
10+
11+
“An Old Man is happy today, he doesn’t complain about anything, smiles, and even his face is freshened up.”
12+
13+
The whole village gathered together. The old man was asked:
14+
15+
Villager: What happened to you?
16+
17+
“Nothing special. Eighty years I’ve been chasing happiness, and it was useless. And then I decided to
18+
live without happiness and just enjoy life. That’s why I’m happy now.” – An Old Man
19+
20+
Moral of the story:
21+
Don’t chase happiness. Enjoy your life.

story2.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This story is about the boy who live.
2+
His name is Harry Potter.
3+
The boy was cursed by "Voldemort".
4+
"Voldemort" Killed Harry's parents.

0 commit comments

Comments
 (0)