mlot

Simple Python Reference List

Feb 16th, 2024
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Simple Python Reference List

Zen of Python (Brief)

  • Beautiful is better than ugly.
  • Simple is better than complex.
  • Complex is better than complicated.
  • Readability counts.

Keywords:

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

Lists:

  • list.append(item)
  • list.extend(list)
  • list.insert(location, item)
  • list.remove(item)
  • list.pop(item)
  • list.index(item)
  • list.count(item)
  • list.sort()
  • list.reverse()

Files:

  • openFile = open(filename, mode)
  • openFile.read(amount)
  • openFile.readlines(amount)
  • openFile.write(data)
  • openFile.close()

File Access Modes

  • 'r' - Read Access
  • 'w' - Write Access (overwites existing)
  • 'a' - Append Access

Example: with open('filename', 'mode') as openFile:

Strings:

  • string.capitalize()
  • string.center(width)
  • string.count(substring)
  • string.decode()
  • string.encode()
  • string.endswith(substring)
  • string.find(substring)
  • string.format()
  • string.isalnum()
  • string.isalpha()
  • string.isdecimal()
  • string.isdigit()
  • string.islower()
  • string.isnumeric()
  • string.isspace()
  • string.istitle()
  • string.isupper()
  • string.join()
  • string.ljust(width)
  • string.lower()
  • string.lstrip()
  • string.replace(original, replacement)
  • string.rjust(width)
  • string.rstrip()
  • string.startswith(substring)
  • string.strip()
  • string.swapcase()
  • string.title()
  • string.upper()

Manipulate Strings

  • '+' - Join Strings Together
  • '*' - Repeat String X Times
  • '[ ]' - Select Character at Index
  • '[ : ]' - Range Select Characters in Index
  • 'in' - Returns True if Character(s) in String
  • 'not in' - Returns True if Character(s) not in String

Arithmetic Operators

  • '+' Addition
  • '-' Subtraction
  • '*' Multiplication
  • '/' Division
  • '%' Modulus
  • '//' Floor Division
  • '**' Exponent

Add an '=' after an operator to assign values. For example, 'a += b' assigns the value of 'a + b' to the variable 'a'.

Comparisons

  • '==' Equality
  • '!=' Not Equal
  • '>' Greater Than
  • '<' Less Than
  • '>=' Greater Than or Equal
  • '<=' Less Than or Equal

Data Types

  • int(x) - Integer Type
  • str(x) - String Type
  • float(x) - Floating Point
  • chr(x) - Character
  • unichr(x) - Unicode Character
  • ord(x) - Integer value
  • hex(x) - Hexadecimal String
  • oct(x) - Octal String

Datetime strftime() Method

  • '%A' - Full Weekday Name
  • '%B' - Full Month Name
  • '%c' - Local Date and Time
  • '%d' - Day of the month number
  • '%f' - Microsecond
  • '%H' - Hour number
  • '%j' - Day of the Year
  • '%m' - Month number
  • '%M' - Minute number
  • '%p' - AM/PM Local
  • '%S' - Second number
  • '%w' - Week Day number
  • '%X' - Local Time
  • '%Y' - Year
  • '%Z' - Timezone Name

Regular Expression Metacharacters

  • '.' - Any Character
  • '^' - First Characters
  • '$' - Last Characters
  • '*' - Zero or More Characters
  • '+' - One or More Characters
  • '?' - Zero or One Characters
  • '{ }' - Multiple Characters
Add Comment
Please, Sign In to add comment