jagomart
digital resources
picture1_Python Pdf 183878 | Pythoncheatsheet


 127x       Filetype PDF       File size 0.14 MB       Source: canberragpn.github.io


File: Python Pdf 183878 | Pythoncheatsheet
gpn python syntax cheat sheet import useful modules variables import math number 5 import re stringofwords rock for the win rock rock rock listofnums printing results listofstrings print the user ...

icon picture PDF Filetype PDF | Posted on 31 Jan 2023 | 2 years ago
Partial capture of text on file.
                                                                        GPN Python Syntax Cheat Sheet
       # Import useful modules                                    # Variables:
       import math                                                number = 5
       import re                                                  stringOfWords = ‘Rock for the win! Rock rock ROCK!’
                                                                  listOfNums = [1, 2, 3, 4, 5, 6]
       # Printing results:                                        listOfStrings = [‘Rock for the win!’, ‘paper sucks’, ‘scissors is ok’, ‘I can beat the computer’]
       print (‘The user input‘, text, ‘but                        dictionaryFood = [’fruit’:’apple’, ’veg’:’carrot’, ’treat’:’chocolate’, ’drink’:’coke’]                                                   # key:value
       really we think’, stringOfWords)                           # Comments:
                                                                  # Write some comments here to explain what you’re doing
       # Loops                                                                                                                                                 # Getting input:    
       while True:                                                                                                                                             text = input(‘Please enter some text: ‘)
                  entry = input('Type end when finished:  ')
                  if entry == 'end':                                              # string match                                                               # If-Else Statements
                            print ('The end is here')
                            break                                                 # breaks out of loop entirely                                                if number == 5:
                  print ('We\'re still going ..')                                 # Note: escape character: \                                                             print (‘Number equals 5’)
       # Pattern matching & Regular Expressions (RegEx)                                                                                                        elif number < 5:
       import re                                                                                                                                                          print (‘Number is small’)
       stringOfWords = ‘Rock is awesome!’                                                                                                                      else       print (‘Number must be larger 
       if re.search(‘rock’, stringOfWords, re.IGNORECASE):                                                                                                     than 5!’)
                                                                                                                                                                 .   Any character          [a-zA-Z]   Set of upper or lowercase 
                  print (‘rock, Rock or ROCK is in the string’)                                                                                                                                        letters
       if re.search(‘^Paper | Rock | Scissors’, text):                                                                                                           \d  Digits [0-9]           \w         Alphanumeric [a-zA-Z0-9_ ]
       print (‘The line starts with a valid word’)                                                                                                               \s  Whitespace (space,  |             Or
                                                                                                                                                                     tab, newline)
       substitutedString = re.sub('Rock','paper', stringOfWords)                                                                                                 ^   Start of line          $          End of line
       print ('Words are: ', substitutedString)                                                                   # paper is awesome                             +   Matches 1 or more      *          Matches 0 or more of previous 
                                                                                                                                                                     of previous char                  char
                                                              GPN Python Syntax Cheat Sheet Notes
       # Converting strings to integers                                # For statements
       string = str(number)                                            for string in listOfStrings:
                                                                                  print (string)
       # Converting integers to strings: for number in range(1,6):
       number = int(text)                                                         if number %2 == 1:                                                        #  1 modulo 2 = 1,  2 modulo 2 = 0, ..
                                                                                            continue                                              # skips to start of next loop
       # Maths:                                                                   print (number)                                                            # prints  2  4  6 
       import math
       multiply = number * (price + 0.50)
       square = number ** 2
       intDivide = 7 / 2                                                          # 3            (only whole integers!)
       floatDivide = 7.0/2.0                                                                # 3.5
       number+=1                                                                            # increments number
       circleArea = math.pi*(radius**2)
       x = math.pow(y,z)                                                                               # raises y to the power of z
       print (‘-’ * 20)                                                                     #-------------------
       string = ‘hi there’
       print (string * 3)                                                                   # hi there hi there hi there 
       # Random number generation
       import random
       random.
       options = (first, second, third)
       result = random.choice(options)                                                      # choose from a set
The words contained in this file might help you see if this file matches what you are looking for:

...Gpn python syntax cheat sheet import useful modules variables math number re stringofwords rock for the win listofnums printing results listofstrings print user input text but dictionaryfood key value really we think comments write some here to explain what you doing loops getting while true please enter entry type end when finished if string match else statements is break breaks out of loop entirely still going note escape character equals pattern matching regular expressions regex elif small awesome must be larger search ignorecase than any set upper or lowercase in letters paper scissors d digits w alphanumeric line starts with a valid word s whitespace space tab newline substitutedstring sub start words are matches more previous char notes converting strings integers str range int modulo continue skips next maths prints multiply price square intdivide only whole floatdivide increments circlearea pi radius x pow y z raises power hi there random generation options first second third ...

no reviews yet
Please Login to review.