0% found this document useful (0 votes)
40 views25 pages

Python Cheet Sheet

This document provides information about the Faker library in Python. It can generate fake random data like names, addresses, emails, text, and more. It demonstrates how to print fake data, create a JSON file of fake student data, print fake names and countries in Hindi, and generate credit cards, profiles, and data in other languages. It also discusses GUI programming in Python using Tkinter to create labels, buttons, entries, and handle events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views25 pages

Python Cheet Sheet

This document provides information about the Faker library in Python. It can generate fake random data like names, addresses, emails, text, and more. It demonstrates how to print fake data, create a JSON file of fake student data, print fake names and countries in Hindi, and generate credit cards, profiles, and data in other languages. It also discusses GUI programming in Python using Tkinter to create labels, buttons, entries, and handle events.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Faker Library

• Faker (https://pypi.python.org/pypi/Faker) is a Python


package that generates Fake (Random) data for you.
• Faker has the ability to print/get a lot of different fake
data, for instance, it can print fake name, address,
email, text, etc.
from faker import Faker
fake = Faker()
print (fake.email())
print(fake.country())
print(fake.name())
print(fake.text())
print(fake.latitude(),
fake.longitude())
print(fake.url())
from faker import Faker
import json # To create a json file
from random import randint # For student id JSON of
fake = Faker()
def input_data(x): 100
student_data ={}
for i in range(0, x):
students
student_data[i]={}
student_data[i]['id']= randint(1, 100)
student_data[i]['name']= fake.name()
print(student_data)

with open('students.json', 'w') as fp:


json.dump(student_data, fp)

def main():
number_of_students = 10
input_data(number_of_students)
main()
10 fake Names and Countries in Hindi language
from faker import Faker
fake = Faker('hi_IN') #'hi_IN' changed the language
for i in range(0, 10):
print('Name->', fake.name(), ', ', fake.country())

https://faker.readthedocs.io/en/master/
ar_EG - Arabic (Egypt) hr_HR - Croatian
ar_PS - Arabic (Palestine) hu_HU - Hungarian Language
ar_SA - Arabic (Saudi Arabia) hy_AM - Armenian and
it_IT - Italian
bg_BG - Bulgarian Country
ja_JP - Japanese
bs_BA - Bosnian Codes
ka_GE - Georgian (Georgia)
cs_CZ - Czech ko_KR - Korean
de_DE - German lt_LT - Lithuanian
dk_DK - Danish lv_LV - Latvian
el_GR - Greek ne_NP - Nepali
en_AU - English (Australia) nl_NL - Dutch (Netherlands)
en_CA - English (Canada) no_NO - Norwegian
en_GB - English (Great Britain) pl_PL - Polish
pt_BR - Portuguese (Brazil)
en_NZ - English (New Zealand)
pt_PT - Portuguese (Portugal)
en_US - English (United States)
ro_RO - Romanian
es_ES - Spanish (Spain) ru_RU - Russian
es_MX - Spanish (Mexico) sl_SI - Slovene
et_EE - Estonian sv_SE - Swedish
fa_IR - Persian (Iran) tr_TR - Turkish
fi_FI - Finnish uk_UA - Ukrainian
fr_FR - French zh_CN - Chinese (China)
hi_IN - Hindi zh_TW - Chinese (Taiwan)
Create Fake Profiles

from faker import Faker


fake = Faker()
for _ in range(100):
print(fake.profile())
Create Credit Cards (VISA/Master/Maestro)
from faker import Faker
fake = Faker()

print (fake.credit_card_expire(start="now", end="+10y", date_format="%m/%y"))

print (fake.credit_card_full(card_type=None))

print (fake.credit_card_number(card_type=None)) Expiry Date


of Card
print (fake.credit_card_provider(card_type=None)) (Next 10
Years)
print (fake.credit_card_security_code(card_type=None))
GUI Programming
First GUI Screen
import tkinter

window = tkinter.Tk()

window.title("Welcome to All")

window.mainloop()
Create a label widget
import tkinter
window = tkinter.Tk()
window.title("Welcome")
lbl = tkinter.Label(window, text="Hello")
lbl.grid(column=0, row=0)
window.mainloop()

Set label font size


import tkinter
window = tkinter.Tk()
window.title("Welcome")
window.geometry('350x200')

lbl = tkinter.Label(window, text="Hello")


lbl.grid(column=0, row=0)

btn = tkinter.Button(window, text="Click Me")


btn.grid(column=1, row=0)

window.mainloop()
Assorted Buttons

import tkinter
parent = tkinter.Tk()
redbutton = tkinter.Button(parent, text = "Red", fg = "red")
redbutton.pack( side = tkinter.LEFT)
greenbutton = tkinter.Button(parent, text = "Black", fg = "black")
greenbutton.pack( side = tkinter.RIGHT )
bluebutton = tkinter.Button(parent, text = "Blue", fg = "blue")
bluebutton.pack( side = tkinter.TOP )
blackbutton = tkinter.Button(parent, text = "Green", fg = "red")
blackbutton.pack( side = tkinter.BOTTOM)
parent.mainloop()
Multi-Window Interface
import tkinter
window = tkinter.Tk()
window.title("Welcome")
window.geometry('350x200')

lbl = tkinter.Label(window, text="Hello")


lbl.grid(column=0, row=0)

def showmessage():
def window2destroy():
window2.destroy()

window2 = tkinter.Tk()
window2.title("Welcome")
window2.geometry('100x100')
btn2 = tkinter.Button(window2, text="Exit", command=window2destroy)
btn2.grid(column=1, row=0)

btn = tkinter.Button(window, text="Click Me", command=showmessage)


btn.grid(column=1, row=0)
window.mainloop()
Entry Boxes
import tkinter
top = tkinter.Tk()
top.geometry("400x250")
name = tkinter.Label(top, text = "Name").place(x = 30,y = 50)
email = tkinter.Label(top, text = "Email").place(x = 30, y = 90)
password = tkinter.Label(top, text = "Password").place(x = 30, y = 130)
e1 = tkinter.Entry(top).place(x = 80, y = 50)
e2 = tkinter.Entry(top).place(x = 80, y = 90)
e3 = tkinter.Entry(top).place(x = 95, y = 130)
top.mainloop()
Event: Button Click
import tkinter as tk
window = tk.Tk()
window.title("Welcome")
window.geometry('350x200')
lbl = tk.Label(window, text="Hello")
lbl.grid(column=0, row=0)

def clicked():
lbl.configure(text="Button was clicked !!")

btn = tk.Button(window, text="Click Me", command=clicked)


btn.grid(column=1, row=0)
window.mainloop()
Get input using Entry class (Tkinter textbox)
import tkinter as tk
window = tk.Tk()
window.title("Welcome")
window.geometry('350x200')
lbl = tk.Label(window, text="Hello")
lbl.grid(column=0, row=0)
txt = tk.Entry(window,width=10)
txt.grid(column=1, row=0) Width of
def clicked(): Textbox
lbl.configure(text="Button was clicked !!")
btn = tk.Button(window, text="Click Me", command=clicked)
btn.grid(column=2, row=0)
window.mainloop()
Getting User Input and Evaluations
import tkinter as tk
root=tk.Tk()

def clickhere():
getg2=g2.get()
r.config(text=getg2)

g2=tk.IntVar()

g1=tk.Label(root, text="A").grid(row=1, column=0)

entryg2=tk.Entry(root, textvariable=g2).grid(row=2, column=0)

r=tk.Label(root, text=“Output”)
r.grid(row=3, column=0)

s=tk.Button(root, text="Click", command=clickhere).grid(row=4, column=0)


root.mainloop()
Basic Calculations
import tkinter as tk
root=tk.Tk()

def clickhere():
gp=grosspay.get()
deduc=deductions.get()
taxable=gp-deduc
taxablelabel.config(text=taxable)

grosspay=tk.IntVar()
deductions=tk.IntVar()

grosspaylabel=tk.Label(root, text="Gross Pay").grid(row=1, column=0)


deductionslabel=tk.Label(root, text="Deductions").grid(row=2, column=0)

entrygp=tk.Entry(root, textvariable=grosspay).grid(row=1, column=2)


entrydeduc=tk.Entry(root, textvariable=deductions).grid(row=2, column=2)

taxablelabel=tk.Label(root)
taxablelabel.grid(row=3, column=0)

s=tk.Button(root, text="Calculate", command=clickhere).grid(row=4, column=0)

root.mainloop()
import tkinter as tk
from functools import partial

def call_result(label_result, n1, n2):


num1 = (n1.get())
num2 = (n2.get())
result = int(num1)+int(num2)
label_result.config(text="Result = %d" % result)
return

root = tk.Tk()
root.geometry('400x200+100+200')
root.title('Calculator')
number1 = tk.StringVar() Calculations
number2 = tk.StringVar()
labelNum1 = tk.Label(root, text="A").grid(row=1, column=0) on User
labelNum2 = tk.Label(root, text="B").grid(row=2, column=0)
labelResult = tk.Label(root) Inputs
labelResult.grid(row=7, column=2)

entryNum1 = tk.Entry(root, textvariable=number1).grid(row=1, column=2)


entryNum2 = tk.Entry(root, textvariable=number2).grid(row=2, column=2)

call_result = partial(call_result, labelResult, number1, number2)

buttonCal = tk.Button(root, text="Calculate", command=call_result).grid(row=3, column=0)


root.mainloop()
SMS Gateway / API

https://www.fast2sms.com
Create Account
And
Click on
DEV API
Click Here for Code
Select Programming Language
Python Code with API
import requests

url = "https://www.fast2sms.com/dev/bulk"

payload =
"sender_id=FSTSMS&message=This%20is%20a%20test%20message&langua
ge=english&route=p&numbers=9034001978"
headers = {
'authorization': “API-KEY",
'Content-Type': "application/x-www-form-urlencoded",
'Cache-Control': "no-cache",
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)
GAURAV KUMAR

Magma Research and Consultancy Services

Mobile Numbers : +91-9416366178, +91-9034001978

E-mail : [email protected]

http://www.gauravkumarindia.com

You might also like