0% found this document useful (0 votes)
2 views

python-cheatsheet

This document is a cheat sheet for Python programming, covering basic operations, functions, comparison and boolean operations, input/output, loops, and variables. It also includes starter code for creating graphics using Tkinter, along with commands for drawing shapes and text on a canvas. Optional parameters for customizing graphics are also provided.

Uploaded by

mohammed
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)
2 views

python-cheatsheet

This document is a cheat sheet for Python programming, covering basic operations, functions, comparison and boolean operations, input/output, loops, and variables. It also includes starter code for creating graphics using Tkinter, along with commands for drawing shapes and text on a canvas. Optional parameters for customizing graphics are also provided.

Uploaded by

mohammed
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/ 2

SAMS Programming Workshop - Python Cheat Sheet

Basic Operations Functions


1​ ​+​ ​2​ ​# addition # sets up a new function, name
2​ ​-​ ​1​ ​ # subtraction # it takes params p1, p2, ... as input
2​ ​*​ ​3​ ​ # multiplication # it returns returnVal as output
def ​name​(​p1, p2, ...​):
4​ ​/​ ​2​ ​ # division
# do stuff here
2​ ** ​3​ ​ # raise to power ​return ​returnVal
"a"​ ​+​ ​"b"​ ​ # combines two strings
# call a function name on vals a, b
Comparison Operations result ​= ​name​(a
​ , b​)
2​ ​< ​3​ ​# less than
"a"​ ​> ​"b"​ ​ # greater than Conditionals
"a"​ ​<= ​"b"​ ​ # less than or equal to # only runs code in the block
# if the expression is True
2​ ​>= ​3​ ​ # greater than or equal to
if ​option ​== True:
"a"​ ​== ​"b"​ ​ # is equal to ​print(​"here"​)
2​ ​!= ​3​ ​ # is not equal to
# run code in a single branch
Boolean Operations # based on the boolean expressions
True and False #​ both must be True if ​option1 ​== True:
True or False ​ #​ at least one True ​print(​"branch 1"​)
elif ​option2 ​== True:
not True ​ ​ flips boolean value
#
​print(​"branch 2"​)
else:
Input and Output ​print(​"branch 3"​)
# prints to the console
print(​"Hello World"​) Loops
# loops until the test is False
# prints multiple items i ​= ​start
print(​"a"​, ​"b"​, ​"c"​) while ​i ​<​ end​:
​print(​i​)
i ​=​ i ​+​ step
# asks the user for input in console
s ​=​ ​input​(​"Enter a thing: "​) # breaks out of loop at some input
while True:
Built-in Functions val ​= ​input​(​"Enter: "​)
x ​=​ ​int​(​s​)​ ​# casts a string to an int ​if ​val ​== ​"something"​:
x ​ = ​float​(​s​)​ ​# casts a string to a float ​break
s ​ = ​str​(​x​)​ ​# casts an int to a string ​print(​val​)
x ​ = ​len​(​s​)​ ​# finds num of letters in str
# loops over the given range
x ​ = ​max​(​a, b, c​)​ ​# max of given numbers # with start, end, step
x​ = ​min​(​a, b, c​)​ ​# min of given numbers for ​i ​in ​range​(​start, end, step​):
x ​ = ​round​(​y, d​)​ ​# rounds y to d sig-digs ​print(​i​)

Variables # loops over the chars in a string


# assigns var x to hold the value 5 for ​c ​in ​string​:
x ​=​ ​5 ​print(​c​)

# uses the value in var x


print(​x ​-​ ​2​)
SAMS Programming Workshop - Python Cheat Sheet

Tkinter Starter Code


# use this to create a window to draw graphics in
from ​tkinter ​import ​*
root ​=​ ​Tk()
width, height ​=​ ​400​, ​400
canvas ​=​ ​Canvas(​root, ​width=​width, ​height=​height​)
canvas.​configure(bd=​0​, h ​ ighlightthickness=​0​)
canvas.​pack()

# Put your code here!

root.​mainloop()

Tkinter Graphics
# draws a rectangle between coords (left, top) and (right, bottom)
canvas.​create_rectangle(​left, top, right, bottom​)

# draws an oval in the bounding box with coords (L, T) and (R, B)
canvas.​create_oval(​L, T, R, B​)

# draws the given text centered at the given coordinate (x, y)


canvas.​create_text(​x, y, ​text=​"sample"​)

# draws a line between the given points (x1, y1) and (x2, y2)
canvas.​create_line(​x1, y1, x2, y2​)

# draws a polygon by connecting the given points with lines


canvas.​create_polygon(​x1, y1, x2, y2, x3, y3, ...​)

Tkinter Optional Parameters


# changes the color of the drawn shape
canvas.​create_rectangle(​left, top, right, bottom, ​fill=​"red"​)

# changes the outline color of the drawn shape


canvas.​create_rectangle(​left, top, right, bottom, ​outline=​"yellow"​)

# changes the pixel width of the drawn line or shape's border


canvas.​create_line(​x1, y1, x2, y2, ​width=​5​)

# changes the font of the drawn text- "font-name font-size font-style"


canvas.​create_text(​x, y, ​text=​"sample"​, ​font=​"Times 30 bold"​)

# changes the anchor point for the drawn text


canvas.​create_text(​x, y, ​text=​"sample"​, ​anchor=​NW​)

You might also like