gcse.compt.sci.programming.u2

Cards (27)

  • 2D list:

    letters = [["A", "B"], ["C", "D"], ["E", "F"]]
  • Pythons dictionary keys are:

    Immutable (cannot be modified after creation)
  • Python's dictionary values are:

    Mutable (can be modified after creation)
  • Integer division:
    print(13//5)
    ans = 2
    ignores remainder
  • Modulus Division:
    print(13%5)
    ans = 3
    displays remainder
  • Selection
    If statements
  • Condition Controlled Iteration:

    Section of code is repeated in sequence until a condition is met
    While loops
  • Infinite iteration:

    while True:
    num_1 = int(input("Enter a number between 1 and 100:"))
    if num_1 >=0 and num_1 <= 100:
    print("Number is in range")
    break
    elif num_1 < 0 or num_1 > 100:
    print("Number is out of range")
  • Infinite iteration with test expression:

    num_2 = int(input("Enter a number between 1 and 100: "))
    while num_2 < or num_2 > 100:
    print("Out of range")
    num_2 = int(input("Enter a number between 1 and 100: "))
    print("Number is in range")
  • Count-controlled iteration:
    for loops
  • range() function with a string 

    for i in range(len("Hello")):
    print("Hello"[i])
    outputs:
    h
    e
    l
    l
    o
  • For loop with string:

    for char in "Python":
    print(char)
  • Subroutines, returning values:

    def inputs():
    num_1 = int(input(""))
    num_2 = int(input(""))
    return num_1, num_2
    #main program begins here
    num_1, num_2 = inputs()
  • Subroutines with parameters:

    def inputs(num_3, num_4):
    print(num_3 + num_4)
    #main program begins here
    num_3 = int(input(""))
    num_4 = int(input(""))
    addition(num_3,num_4)
  • Capitalise first letter of string:

    string = "gFHhhfsAh"
    string = string.capitalize()
    print(string)
  • Capitalise whole string:

    string = "gFHhhfsAh"
    string = string.upper()
    print(string)
    outputs: GFHHHFSAH
  • Make whole string lowercase:

    string = "gFHhhfsAh"
    string = string.lower()
    print(string)
    outputs: gfhhhfsah
  • Find a character in a string:

    string.find(char)
  • Replace character with another:

    string.centre("s"(old), "3"(new))
  • Capitalize the first letter of every word:

    string.title()
  • String is methods:

    .islower():
    Returns true if all characters are lowercase
    .isupper():
    Returns true if all characters are uppercase
    .isdigit():
    Returns true if all characters are digits
    .isalpha():
    Returns true if all characters are part of the Roman alphabet
    .isalnum():
    Returns true if all characters are alphabets or digits
  • List methods:

    .insert()
    Inserts the specified value at the specified position.
    .pop()
    Removes the element at the specified position.
    .copy()
    Returns a copy of the specified list.
    .append()
    Add an element to the end of the list.
    .index()
    Returns the position at the first occurrence of the specified value.
    .sort()
    Sorts the list into ascending order.
    .remove()
    Removes an element from a list.
    .count()
    Returns the number of elements with the specified value.
  • Dictionary methods:

    .get()
    Returns the value of a specified key from a dictionary.
    .pop()
    Removes a key-value pair from the dictionary.
    .clear()
    Removes all key-value pairs from a dictionary.
    .keys()
    Returns list of keys.
    .values()
    Returns a list of values.
    .items()
    Returns a list of key-value pairs.
  • Random modules

    import random
    num = random.randint(1,100)
    num = randint(1,100)
  • String Slicing
    print(predefinedword[2:7])
    print(predefinedword[-1:6])

    Check for palindrome:
    word = input("")
    if word == word[::-1]:
    print("nice")
    elif word != word[::-1]:
    print("bad")

    Reverse order of word:
    word = "England"
    print(word[::-1])
  • Syntax error:

    Occurs when the programming language has not been followed
    Causes the program to crash immediately
  • Logic error:

    Causes program to produce unexpected results
    Does not cause the program to crash