Data Types and Programming Techniques

This section explains Data Types and Programming Techniques focusing on Data types, Casting, String manipulation, Basic file handling operations, The use of records to store data and SQL, Arrays, Using subprograms to produce structured code, Random number generation.

Data Types

Data types define the kind of data a variable or constant can hold. Common data types include:

  • Integer: Whole numbers (e.g., 5, -10).
  • Float/Real: Numbers with decimal points (e.g., 3.14, -0.5).
  • Boolean: True/False values (e.g., True, False).
  • String: Text (e.g., "Hello, world!").
  • Character: A single letter, digit, or symbol (e.g., 'A', '1').

Casting

Casting is the process of converting a variable from one data type to another.

Examples:

python
int("5") # Converts a string "5" to an integer
float(10)  # Converts an integer 10 to a float
str(3.14)  # Converts a float 3.14 to a string


String Manipulation

Strings are sequences of characters and can be manipulated using various techniques.

Length: Find the number of characters in a string.

python
len("Hello")  # Result: 5


Character Position: Retrieve a character at a specific position (indexing starts at 0 in most languages).

python
"Hello"[1]  # Result: 'e'


Upper and Lowercase: Change the case of a string.

python
"hello".upper()  # Result: 'HELLO'
"HELLO".lower()  # Result: 'hello'


Concatenation: Combine two or more strings.

python
"Hello" + " " + "World"  # Result: 'Hello World'
 

Basic File Handling Operations

File handling is used to store, retrieve, and manipulate data in files.

Opening and Closing Files:

python
file = open("example.txt", "r")  # Open in read mode
file.close() # Close the file


Reading from a File:

python
file = open("example.txt", "r")
data = file.read() # Read the entire file
file.close()


End of File:

  • Reached when there is no more data to read.
  • Typically used in loops to stop reading.

Writing to a File:

python
file = open("example.txt", "w")  # Open in write mode
file.write("Hello, file!")  # Write data to the file
file.close()


Use of Records to Store Data and SQL

Databases, Records, and Attributes

  • Database: A structured collection of data.
  • Record: A single row in a database table.
  • Attribute: A column in a database table (e.g., name, age).

Structured Query Language (SQL)

SQL is used to interact with databases.

Retrieving Data:

sql
SELECT name, age FROM students WHERE age > 16;


Common Commands:

  • SELECT: Retrieve data.
  • INSERT: Add new records.
  • UPDATE: Modify existing records.
  • DELETE: Remove records.

Arrays

Arrays are used to store multiple values under a single variable name.

 Declaring an Array

Define an array with a specific size or populate it directly.

python
numbers = [1, 2, 3, 4, 5]  # Declare and assign values


Assigning Values to an Array

Python
numbers[0] = 10 # Assign 10 to the first element


Retrieving Values from an Array

python
print(numbers[0]) # Retrieve the first element


Two-Dimensional Arrays

Arrays within arrays, used for grid-like data.

python
grid = [[1, 2], [3, 4]]  # 2D array
print(grid[0][1]) # Access the second element of the first row


Using Subprograms to Produce Structured Code

Benefits of Using Subprograms

  • Makes code reusable and easier to maintain.
  • Improves readability by breaking the program into logical sections.
  • Reduces duplication of code.

Procedures

Subprograms that perform a task but do not return a value.

python
def greet():
   print("Hello")


Functions

Subprograms that perform a task and return a value.

python
def add(a, b):
    return a + b


Built-in Functions

Predefined functions provided by the programming language.

Examples: len(), range(), abs().

Random Number Generation

Random numbers are often used in games and simulations.

Example:

python
import random
random_number = random.randint(1, 10)  # Generates a random integer between 1 and 10


Summary of Key Concepts

Data Types: Categorise the kind of data stored in variables.

String Manipulation: Includes finding length, accessing characters, and concatenation.

File Handling: Enables reading, writing, and managing files.

Records and SQL: Used for handling structured data in databases.

Arrays: Store multiple values; two-dimensional arrays are used for grid-like structures.

Subprograms: Include procedures and functions, helping to structure code logically.

Random Numbers: Useful for generating unpredictable values.

These concepts are critical for creating efficient and functional programs.

sign up to revision world banner
Southampton University
Slot