0% found this document useful (0 votes)
3 views

8 Programming

The document explains the concepts of variables and constants in programming, detailing their characteristics and usage. It also covers functions and procedures, including their definitions, parameters, and examples in Python, as well as basic operations with arrays. Additionally, it provides examples of common programming tasks such as string manipulation, arithmetic operations, and array handling.

Uploaded by

anish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

8 Programming

The document explains the concepts of variables and constants in programming, detailing their characteristics and usage. It also covers functions and procedures, including their definitions, parameters, and examples in Python, as well as basic operations with arrays. Additionally, it provides examples of common programming tasks such as string manipulation, arithmetic operations, and array handling.

Uploaded by

anish
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

What is a variable?

•A variable is an identifier that can change in the


lifetime of a program
•Identifiers should be:
• In mixed case (Pascal case)
• Only contain letters (A-Z, a-z)
• Only contain digits (0-9)
• Start with a capital letter and not a digit
•A variable can be associated a datatype when it is
declared
•When a variable is declared, memory is
allocated based on the data type indicated
What is a constant?
•A constant is an identifier set once in the
lifetime of a program
•Constants are generally named in all uppercase
characters
•Constants aid the readability and maintainability
•If a constant needs to be changed, it should only
need to be altered in one place in the whole
program
x=0
while x < 5: # Loops as long as x is less than 5
print(x)
x += 1
# Simple Python program to find the length of a string

user_string = input("Enter a string: ")


string_length = len(user_string)
print(f"The length of the string is: {string_length}")

# Simple Python program to extract a substring from a string

user_string = input("Enter a string: ")

start_index = int(input("Enter the starting index: "))

end_index = int(input("Enter the ending index: "))

substring = user_string[start_index:end_index]

print(f"The extracted substring is: '{substring}'")


# Simple Python program to convert a string to uppercase

user_string = input("Enter a string: ")


uppercase_string = user_string.upper()
print(f"The string in uppercase is: '{uppercase_string}'")
What are functions and procedures?

•Functions and procedures are a type of sub program, a sequence of


instructions that perform a specific task or set of tasks. In contrast
to a procedure, a function will return a value back to the main program.

•Procedures and functions are defined at the start of the code


•Sub programs are often used to simplify a program by breaking it into smaller,

more manageable parts

•Sub programs can be used to:


• Avoid duplicating code and can be reused throughout a program
• Improve the readability and maintainability of code
•Parameters are values that are passed into a
sub program
•Parameters can be variables or values and they
are located in brackets after the name of the sub
program
•Sub programs can have multiple parameters
•To use a sub program you 'call' it from the main
program
def add_numbers(x, y):

result = x + y

return result

sum = add_numbers(5, 3) # sum will be 8


def greet_user(name):

print("Hello, " + name + "!")

greet_user("Alice") # Output: Hello, Alice!


1. Convert the following denary (base 10) values into their 8-bit binary equivalent. You
must show your working out. 31,104,210
2. Add the following two 8-bit binary values.

3. A logical shift instruction moves each bit in the binary value left or right. What is the
new value of 00101100 when a logic shift right by two is performed? What is the new
value of 00011100 when a logic shift left by three is performed?
4. If 84 is represented as a denary, calculate its hexadecimal value.
5. Convert the following binary values into hexadecimal representation. 00111100 ,
10100101 , 11101111

6. Convert the following hexadecimal values into binary representation 98,E7, BE


# Function to calculate the square of a number
def square(number):
return number * number

# Calling the function


result = square(5)
print("The square of 5 is:", result)
# Procedure to display a greeting message
def greet(name):
print(f"Hello, {name}! Welcome to IGCSE Computer
Science.")

# Calling the procedure


greet("Alice")
# Function to calculate the area of a rectangle
def calculate_area(length, width):
return length * width

# Procedure to display the area


def display_area(length, width):
area = calculate_area(length, width)
print(f"The area of the rectangle with length {length} and width {width} is:
{area}")

# Calling the procedure


display_area(10, 5)
# Function to check if a number is even
def is_even(number):
return number % 2 == 0

# Procedure to display whether a number is even or odd


def check_even_odd():
number = int(input("Enter a number: "))
if is_even(number):
print(f"{number} is even.")
else:
print(f"{number} is odd.")

# Calling the procedure


check_even_odd()
Many programming language development systems include
library routines that are ready to incorporate into a program

will need to use these library routines in your programs


Creating and Displaying an Array

# Create an array (list) of numbers


numbers = [10, 20, 30, 40, 50]

# Display the array


print("Array elements:", numbers)
Accessing Elements in an Array

# Create an array (list) of fruits


fruits = ["Apple", "Banana", "Cherry", "Date", "Elderberry"]

# Access and display specific elements


print("First fruit:", fruits[0]) # Access the first element
print("Third fruit:", fruits[2]) # Access the third element
Adding and Removing Elements
# Create an empty array
colors = []

# Add elements to the array


colors.append("Red")
colors.append("Green")
colors.append("Blue")

# Display the array after adding elements


print("Colors after adding:", colors)

# Remove an element from the array


colors.remove("Green")

# Display the array after removing an element


print("Colors after removing:", colors)
Iterating Through an Array

# Create an array of numbers


numbers = [1, 2, 3, 4, 5]

# Iterate and display each element


print("Array elements:")
for number in numbers:
print(number)
Finding the Largest Number in an Array
# Create an array of numbers
numbers = [12, 45, 7, 34, 89, 23]

# Find and display the largest number


largest = max(numbers)
print("The largest number is:", largest)
Summing Elements in an Array

# Create an array of numbers


numbers = [10, 20, 30, 40, 50]

# Calculate the sum of elements


total = sum(numbers)

# Display the sum


print("The sum of the numbers is:", total)
Searching for an Element

# Create an array of names


names = ["Alice", "Bob", "Charlie", "Diana"]

# Search for a specific name


search_name = "Charlie"
if search_name in names:
print(f"{search_name} is in the array.")
else:
print(f"{search_name} is not in the array.")
Sorting an Array

# Create an array of numbers


numbers = [45, 12, 89, 23, 7]

# Sort the array in ascending order


numbers.sort()

# Display the sorted array


print("Sorted array:", numbers)
Reversing an Array

# Create an array of numbers


numbers = [10, 20, 30, 40, 50]

# Reverse the array


numbers.reverse()
a
# Display the reversed array
print("Reversed array:", numbers)

You might also like