Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 332bc20

Browse files
committed
Push Python Folder
1 parent a7490cc commit 332bc20

File tree

7 files changed

+238
-0
lines changed

7 files changed

+238
-0
lines changed

Python/Fundamentals_Python.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name = "Afif"
2+
print(type(name))
3+
print(type(name) == str)
4+
print(isinstance(name, str))
5+
age=20
6+
print("Age is",isinstance(age, int))
7+
Age=float(age)
8+
print("Age is float:",isinstance(Age, int))
9+
num="21"
10+
print("AGE = ",num," is str: ",isinstance(num, str))
11+
AGE = int(num)
12+
print("AGE = ",AGE," is int: ",isinstance(AGE, int))
13+
14+
#is Identity operator Returns true while comparing two similar objects
15+
#in membership operator Tells if a value is present in a list, ssequence etc.
16+
def is_adult(a):
17+
return True if a>18 else False #Ternary operator
18+
print(is_adult(age))
19+
print("Afif". upper())
20+
print("AfIF".lower())
21+
print("AfiF person".title())
22+
print("afif".islower())
23+
#isalpha() - check if string only contains characters & is not empty
24+
#isalnum() - check if string contains characters or digits
25+
#isdecimal() - check if string contains only digits
26+
#lower() - lowercase verdsion of a string
27+
#upper() - uppercase version of a string
28+
#startswith() - to check if the string starts with a specific substring
29+
#endswith() - to check if the string ends with a specific substring
30+
#replace() - replaces a part of a string
31+
#split() - separates a string at a specific character separator
32+
#strip() - trim whitespaces from a string
33+
#join() - append new letters to a string
34+
#find() - find position of a substring
35+
print(name[-2])
36+
string = "he is cool"
37+
print(string[:5])
38+
print(string[3:8])
39+
done = False
40+
print(type(done)==bool)
41+
if done:
42+
print("Yes")
43+
else:
44+
print("No")
45+
book1_read = True
46+
book2_read = False
47+
read_any_book = any([book1_read, book2_read])
48+
print(read_any_book)
49+
read_all_book = all([book1_read, book2_read])
50+
print(read_all_book)

Python/GUI Basics/Test2.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#basic window with widgets
2+
'''
3+
Widgets are building blocks of Tkinter
4+
i.e. texts,buttons,menus,frames etc.
5+
Understanding the widgets and controlling them is key to mastering any GUI framework
6+
7+
TKinter has 2 sets of widgets
8+
tk widgets were the og
9+
ttk widgets were added later on (more modern)
10+
'''
11+
import tkinter as tk
12+
from tkinter import ttk
13+
14+
def button_func():
15+
print('A button was clicked')
16+
def print_hello():
17+
print('hello')
18+
19+
#crete window - call tk.Tk() function & its gonna return the actual
20+
# window in which we place everything in
21+
window = tk.Tk()
22+
#to display a certain variable - variable.mathod()
23+
window.title('Window & Widgets')
24+
#set size of window geometry('widthxheight')
25+
window.geometry('800x500')
26+
27+
#ttk widgets
28+
label = ttk.Label(master = window, text='Thi is a test')
29+
label.pack()
30+
31+
#tk.text
32+
text = tk.Text(master = window)
33+
text.pack()
34+
#tk.Text(master = window) only cretes the widget & tells what the parent is. Does not place it visually.
35+
#the method .pack() takes a widget and then packs it all the way on the top center like a stack
36+
37+
#ttk entry
38+
entry = ttk.Entry(master=window)
39+
entry.pack()
40+
41+
text_label = ttk.Label(master = window, text='My label')
42+
text_label.pack()
43+
44+
#ttk button - never call a function in command as we don't want the button to call the function without getting clicked
45+
button = ttk.Button(master=window, text='A Button', command=button_func)
46+
button.pack()
47+
48+
#exercise
49+
#text label
50+
51+
#button
52+
#button_hello = ttk.Button(master=window, text='Print Hello', command=print_hello)
53+
button_hello = ttk.Button(master=window, text='Print Hello', command=lambda:print('hello'))
54+
button_hello.pack()
55+
56+
#run
57+
window.mainloop()
58+
'''
59+
mainloop - 1. Updates to GUI
60+
[Wriiten text or updatation of widgets actually gets displayed]
61+
2. Checks for events [Button clicks, mouse movements, closing the window]
62+
without mainloop app can't run
63+
'''

Python/GUI Basics/Test3.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#Getting widget data & changing widgets
2+
'''
3+
Two major ways to get data from a widget
4+
i.Tkinter variables [Should be used most of the time] (A bit advanced)
5+
ii.get()
6+
7+
Lots of widget have obvious data that the user would want to get
8+
hence there is a get method
9+
For example, entry has a get method that returns its text
10+
11+
Widgets can be updated with config [Every widget has the config method]
12+
Label.config(text='new text')
13+
Label['text']='some new text'
14+
'''
15+
import tkinter as tk
16+
from tkinter import ttk
17+
18+
def btn_func():
19+
#get the content of the entry
20+
#print(entry.get())
21+
entry_text=entry.get()
22+
23+
#update the label
24+
label['text']=entry_text
25+
# entry['state']='disabled'
26+
27+
#window
28+
window=tk.Tk()
29+
window.title('Getting & setting widgets')
30+
31+
#widgets
32+
label=ttk.Label(master=window, text='Label')
33+
label.pack()
34+
35+
entry=ttk.Entry(master=window)
36+
entry.pack()
37+
38+
button=ttk.Button(master=window, text='Button', command=btn_func)
39+
button.pack()
40+
41+
42+
#run
43+
window.mainloop()

Python/GUI Basics/test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#tk gives the basic logic
2+
import tkinter as tk
3+
#ttk has all the widgets that we want to use
4+
#from tkinter import ttk
5+
import ttkbootstrap as ttk
6+
def convert():
7+
mile_input = entry_int.get()
8+
km_output = mile_input * 1.61
9+
output_string.set(km_output)
10+
11+
#window
12+
window = ttk.Window(themename='darkly')
13+
window.title('Demo')
14+
#set size of window widthxheight
15+
window.geometry('300x150')
16+
17+
#WIDGET
18+
#title Label is a fancy word for text in tkinter
19+
title_label = ttk.Label(master=window, text='Miles to Kilometers', font='Calibri 24 bold')
20+
#the widget must be in a container [in tkinter, a master which at this moment is Window]
21+
title_label.pack()
22+
#method for showing title_label
23+
24+
#input field
25+
input_frame=ttk.Frame(master=window)
26+
entry_int=tk.IntVar()
27+
entry=ttk.Entry(master= input_frame, textvariable = entry_int)
28+
#pass the function, do not call it as the button is gonna call the function
29+
button=ttk.Button(master=input_frame, text='Convert', command=convert)
30+
entry.pack(side='left', padx=15)
31+
button.pack(side='left')
32+
input_frame.pack(pady=15)
33+
34+
35+
#output
36+
output_string = tk.StringVar()
37+
output_label=ttk.Label(master=window,
38+
text='Output',
39+
font='Calibri 20',
40+
textvariable = output_string)
41+
output_label.pack(pady=5)
42+
#run
43+
window.mainloop()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
file_name = 'text_files\pi_digits.txt'
2+
with open(file_name) as file_object:
3+
lines = file_object.readlines()
4+
pi_string=''
5+
for line in lines:
6+
pi_string += line.strip()
7+
print(pi_string)
8+
print(len(pi_string))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
3.1415926535
2+
8979323846
3+
2643383279

Python/RPS.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import random
2+
def get_choices():
3+
player_choice = input("Enter Rock/Paper/Scissors\n")
4+
options = ["Rock", "Paper", "Scissors"]
5+
computer_choice = random.choice(options)
6+
choices = {"player": player_choice, "comp": computer_choice}
7+
return choices
8+
def check_win(player, comp):
9+
print("You chose " + player + ", computer chose " + comp)
10+
if(player==comp):
11+
return "Its a Tie!"
12+
elif(player == "Rock"):
13+
if comp == "Scissors":
14+
return "Rock smashes Scissors! You Win!"
15+
else:
16+
return "Paper covers Rock! You lose."
17+
elif(player == "Paper"):
18+
if (comp == "Scissors"):
19+
return "Scissors cuts Paper! You lose."
20+
else:
21+
return "Paper covers Rock! You win!"
22+
else:
23+
if (comp == "Rock"):
24+
return "Rock smashes Scissors! You lose."
25+
else:
26+
return "Scissors cuts Paper! You win!"
27+
choices = get_choices()
28+
print(check_win(choices["player"], choices["comp"]))

0 commit comments

Comments
 (0)