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

A3 Python Cheatsheet

The document provides a quick reference to Python concepts including math operations, strings, logic, loops, conditionals, functions, classes and imports. It includes examples of common operations like addition, multiplication, exponents, modulus, comparison operators, if/else statements, for loops over lists and ranges, defining and calling functions, creating and accessing class attributes and methods, and importing modules.

Uploaded by

lee
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)
273 views

A3 Python Cheatsheet

The document provides a quick reference to Python concepts including math operations, strings, logic, loops, conditionals, functions, classes and imports. It includes examples of common operations like addition, multiplication, exponents, modulus, comparison operators, if/else statements, for loops over lists and ranges, defining and calling functions, creating and accessing class attributes and methods, and importing modules.

Uploaded by

lee
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/ 1

# Addition # Multiplication # While less than

1+3 1*3

Maths
# Exponent while(x < y):
# Integer Division
2**3 2//3           x  += 1
# Subtraction           print(x)
1-3 # Division
# Modulus 1/3 # While true with break
2%3
Python Quick Reference while(True):
          x  += 1
# Return True # Return False
print(x)

Logic
elbairaV
# Assign an number True False
x  = 5           if(x>y):

Loops
# Assign String variable not False not True
maker  = “I am the maker ”                    break
(True | False) # or (False | False) # or
# Make a number into a string
str(x) (True & True) #and (True & False) #and
# Combine strings # For loop with default count
newString = maker + “ More string”  for i in range(1,10):
# Return True if comparison holds

Compare
          print(2**i)
# Output a string x > y   # Greater than
print("Hello World ") # For loop with custom count
x < y   # Less
# output a number for i in range(10,-10,-1):
print(x)
s g ni r t S x <= y # Less or equal
          print(2**i)
x >= y # Greater or equal
# output string and number # For loop over list elements
x == y # Equal
print("hello "+str(x)+ " world")
#input x != y # Not equal for i in myList:
myVariable = input(“message”)           print(i)
#to input number eval output if(x > y):

If..else
myVariable = eval(myVariable)            print("x is larger ")
# define Class
elif(x < y):
          print("y is larger ") class myClass:
# Make a list
myList = ["item0 ", "item1 ", "item2 "] else:   # Initialisation

Classes
# Add item to list           print("Equality! ")      def __init__(self,in1,in2):
Lists

myList.append(“item3”)         self.input1 = in1


# Select first item in a list         self.input2 = in2
# define a function
myList[0] def threeTimes(xIn):     def add(self): # Class method
# Select last item in the list           return xIn*3         return self.input1+self.input2

Functions
myList[-1] # Call with input=5 # Run the program
threeTimes(5) myObject = myClass(1,2)
import time # Define Lambda function
# Call function from time t=6 myObject.add ()
Import

time.sleep(1) # Wait for 1 second tMult= lambda a : a*t


# Import single function # Call with a = 5 t=6
from math import sqrt tMult(5)
# Call t=1
sqrt(2) # Call with a = 5 t=1
tMult(5) 
forum.core-electronics.com.au

You might also like