Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Sequence

What is sequence?

  • Sequence is a set of instructions executed one after another

Python example

def calculate_area(length, width):

“””

This function calculates the area of a rectangle

Inputs:

length: The length of the rectangle

width: The width of the rectangle

Returns:

The area of the rectangle

“””

# Calculate area

return area

area = length * width

# ————————————————————————# Main program

# ————————————————————————

length = 5

width = 3

correct_area = calculate_area(length, width)

print(f"Correct area (length * width): {correct_area}")

  • In the example, the sequence of instructions is wrong and would cause a runtime error (opens in a new tab)

  • In the calculate_area() function a value is returned before it is assigned

  • The correct sequence is:

# Calculate area
area = length * width
return area

Selection

What is selection?

  • Selection is a programming construct that allows a program to execute in different ways based on the outcome of a condition

  • Selection is implemented using the if function

  • Nested selection can be used, the use of ‘else‘ and ‘elif‘ is preferable for efficiency and to make logic clearer

Python examples

age = 18
if age >= 18:
print("You are eligible to vote")
else:
print("You are not eligible to vote")

grade = 85

if grade >= 90:

print("Excellent!")

elif grade >= 80:

print("Very Good!")

else:

print("Good luck next time!")

Nested selection

is_registered = True

has_paid = False

if is_registered:

if has_paid:

print("You can access the course materials")

else:

print("Please complete your payment to access materials")

else:

print("Please register for the course first")

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注