Ech0 - 00 / 00 / 00

CheatSheet - Python

Description:



Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects

Loops and conditions



If, else :

a=int(input("please enter a number : "))
if(a>0):
    print("a is positive!")
else:
    print("a is 0 or negative !")
If , elif, else

a=int(input("Enter a number !"))
if(a>0):
    print("a is positive !")
elif(a<0):
    print("a is negative !")
else:
    print("a is equal to zero !")
While Loops :

a=int(input("enter a positive number !"))
while(a<0):
    a=int(input("enter a positive number please !"))

print("a is positive !")
#above, the while loop will run INFINITELY until the value of a becomes positive.
For loops :

for i in range(10):
    print("i = ",i,end=" ")     # prints out 0 1 2 3 4 5 6 7 8 9
                                # WITHOUT going to a new line !
print()                         # prints nothing AND THEN goes to the new line !

Multiple conditions in one go

  a=int(input("enter a number"))
  b=int(input("enter a number"))
  c=int(input("enter a number"))
  d=int(input("enter a number"))
  e=int(input("enter a number"))
  #   |   |     |----------------------> prints out on the screen "Enter a number"
  #   |   |----------------------------> waits for your KEYBOARD INPUT 212315467897
  #   |--------------------------------> converts the string "212315467897" into an INTeger
  if(a<b or c>d):
    print("either a<b OR c>d !")

  while(b==a and a!=c):
    print("b==a AND a!=c")

  if((a<b and b>c) or (a==c and d==e)):
    print("yea")

  while((a>d and c>d and e==a)or(d==e or d==a or d==b)):
    print("yee")

Arrays, 2D Arrays , Strings



Strings :

word  = "panorama"
word2 = input("please enter whatever you want (it's anyway a string)")

for i in range(len(word)):    #i goes from 0 to 7 because len(word) is 8
    print(word[i],end="-")    # prints out  p-a-n-o-r-a-m-a-

for i in range(len(word2)):   #i goes from 0 to something cause len(word2) is something
    print(word[i])            #prints out the character stored at the i index

alphabet="abcdefghijklmnopqrstuvwxyz"
for i in range(len(word2)):
    pos = alphabet.find(word2[i])
    #       |       |     |   |---> the index i
    #       |       |     |-------> of the char IN word2
    #       |       |-------------> FIND the index of ^ (it's first appearance)
    #       |---------------------> INTO ALPHABET
    #-----------------------------> store ^ into the pos variable
    #
    # pos takes the value of the index of the letter in word2
    # that is at the index i of alphabet

    print("the letter: ",word[i],", is at the index ",pos,"of the alphabet !")
    #      the letter:   a        , is at the index  0     of the alphabet !
    #      the letter:   f        , is at the index  5     of the alphabet !
    #      the letter:   g        , is at the index  6     of the alphabet !
    #      the letter:   h        , is at the index  7     of the alphabet !

Arrays:

  array1 = [0] * 10         # 1D array that has 10 slots
  array2 = [0] * 100        # 1D array that has 100 slots
  array3 = [0] * 1000       # 1D array that has 1000 slots

  for i in range(10):
      array1[i]=int(input("enter a number !")) # fills each 10 slots with keyboard input (slow and manual)

  for i in range(100):
      array2[i] = array1[i%10]                 #fills each 100 slots (quickly automatically)

  for i in range(1000):
      array3[i]=array2[i%100]+array1[i%10]
      #      |          |           |-------> i goes from 0 to 9     (100 times)
      #      |          |-------------------> i goes from 0 to 99    (10 times)
      #      |------------------------------> i goes from 0 to 999   (1 time)

  array2D = [[0] * 3] * 6 #2D array : 3 rows 6 columns FILLED WITH ZEROS !!!
  print(array2D)
  for i in range(3):        #rows
      for j in range(6):    #columns
          print(array2D[i][j],end="  ")
      print()               #go at the end of the line

  # the above will print out :
  #
  # 0 0 0 0 0 0
  # 0 0 0 0 0 0
  # 0 0 0 0 0 0

3



If Statement :

if(note_algo > 19.999999999):
  print("ccf algo reussit :)")
  print("ps y'aura surement 99% de cette page au ccf d'algo")
  print(" :] ")

Functions and formatting



Main, functions, etc.

def main():
    print("this is the main function, which calls other functions")
    print(func1(1,2,3,4)) # prints out 10
    print(func2(4,3,2,1,0)) # prints out 0

def func1(a,b,c,d):
    print("here are my items !")
    print(a,b,c,d) # prints 1 2 3 4
    return a+b+c+d

def func2(e,f,g,h,i):
    print(e,f,g,h,i) # prints 4 3 2 1 0
    return e*f*g*h*i

if __name__ =='__main__':
    main()

# this above __main__ IS THE REAL MAIN !!! which will call the main() function