Python cheat sheet

Spread the love

Men cannot survive without cheating, it is in their nature!

This is a part of my Smart Cheat Sheet idea that is fully available at Udemy. I hope you enjoy it!

I will complete the list in the course of time

String Declaration

#Single Quotes(Used when you have double quote inside a string)
print('I am "Beh"')
#Double Quotes (Used when you have single quote inside a string)
print("I'm Beh")
#Triple Single Quotes (Used when you have both single and boule quotes inside a string)
print('''I'm "Beh"''')
#Triple Double Quotes (Used when you have both single and boule quotes inside a string)

Sequence Characters

#  \'  It prints a single quote
print("I\'m Beh")
#  \"  It prints a double quote
print("I'm \"Beh\"")
#  \t  It makes a horizontal tab space between two strings
print("Name\tAge\tGender")
#  \n  Includes a newline character in a string
print("Line1\nLine2\nLine3")

Variable Naming Rules

# 1. Variable names are case-sensitive.
# 2. Variable names should not start with a number.
# 3. Python Keywords are not allowed as variable names.

Keywords List

# and       del       from      not       while
# as        elif      global    or        with
# assert    else      if        pass      yield
# break     except    import    print	 None
# class     exec      in        raise
# continue  finally   is        return 
# def       for       lambda    try

Arithmetic Operators

# Operator					Shorthand form	What It Does
# --------------------------------------------------
# + 	Addition 			+=				Add two operands
# - 	Subtraction 		-=				Subtract right operand from the left 
# * 	Multiplication 		*=				Multiply two operands
# / 	Division 			/=				Divides the operand on left by the one on right.
# % 	Modulus 			%=				Divides the operand on left by the one on right and returns	remainder
# ** 	Exponent 			**=				left operand raised to the power of right
# // 	Floor division 		//=				Performs just like the / division operator, but truncates the result at the decimal point

Operator Precedence

#  Operator			          Description
#---------------------------------------------------------------------------
#  ()				          Parentheses	
#  **				          Exponent
#  * / % //			          Multiply, divide, modulus and floor division
#  + -				          Addition and subtraction
#  <= < > >=				  Comparison operators
#  <> == !=			          Equality operators
#  = %= /= //= -= += *= **=	  Assignment operators	

Exception Handling

try:
  # the code that might throw an exception
except:
  # the code handles the exception
else:
  # this code block executes if the try block does not raise an exception
finally:
  # the code runs anyway

if Statement

if condition:
  pass

if elif Statement

if condition:
  pass
elif condition:
  pass

if else Statement

if condition:
    pass
else:
    pass

if elif else Statement

if condition:
  pass
elif condition:
  pass
else:
  pass

Comparison Operators

#  Operator	Operator Name					Description
#----------------------------------------------------------
#  == 		Equal to						Returns true if values of both the operands are equal
#  != 		Not Equal to 					Returns true if values of both the operands are not equal
#  > 		Greater than 					Returns true if value of the left operand is greater than the right one
#  < 		Lesser than 					Returns true if value of the left operand is smaller than the right one
#  >= 		Greater than or equal to 		Returns true if value of the left operand is greater than or equal to the right one
#  <= 		Less than or equal to			Returns true if value of the left operand is smaller than or equal to the right one

Logical Operators

#  Operator	    Operator Name		Description
#-------------------------------------------------------------------------------------
#  and 		    Logical AND			Returns true when both operands are True
#  or 		    Logical OR 			Returns true when any one of both operand is True
#  not 		    Logical NOT 		Reverses the operand state
#-------------------------------------------------------------------------------------

String Methods

#   function name       description
#-------------------------------------------------------------------------------------------
#   len()               Returns the number of characters in a string
#   capitalize()        Converts the first character of a string to capital letter
#   count()             Returns the number of occurrences of a substring in the given string
#   lower()             Converts a string into lower case
#   upper()	            Converts a string into upper case
#   isspace()           Returns True if all characters in the string are whitespaces
#   replace()           Replaces all or part of a string with another string
#   isdigit()           Returns True if the string consists of digits only

List Methods

#  Function			Description
#--------------------------------------------------------------------------------------------------
#  all()            Returns True if all the Boolean values in the list are True, else returns False
#  any()			Returns True if any of the Boolean values in the list is True
#  append()         Adds an item to the end of the list
#  len()			Returns the numbers of items in a list
#  insert()         Inserts an item at a given position
#  pop([])          Removes the item at the given position in the list
#  clear()          Removes all items from the list.           
#  reverse()        Reverses the elements of the list
#  sort()			Sorts the elements of the list.
#  min()            Returns the smallest item in the list.
#  max()            Returns the largest item in the list.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *