Computer Architecture and Programming
Modern computing systems consist of five primary components: arithmetic logic unit, control unit, memory, input devices, and output devices. The combination of arithmetic logic and control units forms the central processing unit (CPU), responsible for executing instructions and processing data. Programs are essentially organized collections of instructions that direct computer operations. Despite technological advancements, most contemporary computers still adhere to the von Neumann architecture, which emphasizes the separation of memory from processing units and the use of binary data encoding.
Variables and Data Types
Variables serve as data containers in programming, representing memory locations where values can be stored, retrieved, and modified. Python supports various built-in data types and allows creation of custom types.
- Integers: Python handles integers of any magnitude using the int type. It supports binary (0b100), octal (0o100), decimal (100), and hexadecimal (0x100) notations.
- Floating-point numbers: These represent real numbers with decimal points, supporting both standard notation (123.456) and scientific notation (1.23456e2).
- Strings: Textual data enclosed in single or double quotes, with support for multi-line strings using triple quotes and various encoding representations.
- Boolean values: Logical values represented as True or False, which can be explicitly defined or derived from comparison operations.
- Complex numbers: Mathematical complex numbers expressed as 3+5j, where j represents the imaginary unit.
Variable Naming Conventions
Python variable names must adhere to specific rules:
- Composed of letters, digits, and underscores, but cannot start with a digit
- Case-sensitive (value ≠ Value)
- Cannot conflict with Python keywords or built-in identifiers
- Follow PEP 8 guidelines: use lowercase with underscores for multiple words
- Use descriptive names that indicate purpose and content
Working with Variables
Python provides several built-in functions for type checking and conversion:
- type(): Returns the data type of a variable
- int(): Converts values to integers
- float(): Converts values to floating-point numbers
- str(): Converts objects to string representation
- chr(): Returns character from Unicode code point
- ord(): Returns Unicode code point from character
# Basic arithmetic operations with user input
first_value = int(input('Enter first number: '))
second_value = int(input('Enter second number: '))
print(f'{first_value} + {second_value} = {first_value + second_value}')
print(f'{first_value} - {second_value} = {first_value - second_value}')
print(f'{first_value} * {second_value} = {first_value * second_value}')
print(f'{first_value} / {second_value} = {first_value / second_value:.2f}')
print(f'{first_value} // {second_value} = {first_value // second_value}')
print(f'{first_value} % {second_value} = {first_value % second_value}')
print(f'{first_value} ** {second_value} = {first_value ** second_value}')
Practical Exercises
# Temperature conversion: Fahrenheit to Celsius
fahrenheit_temp = float(input("Enter temperature in Fahrenheit: "))
celsius_temp = (fahrenheit_temp - 32) / 1.8
print(f"{fahrenheit_temp:.1f}°F = {celsius_temp:.1f}°C")
# Circle calculations: circumference and area
circle_radius = float(input("Enter circle radius: "))
circumference = 2 * 3.1416 * circle_radius
area = 3.1416 * circle_radius ** 2
print(f"Circumference: {circumference:.2f}")
print(f"Area: {area:.2f}")
# Leap year detection
input_year = int(input('Enter year: '))
is_leap_year = (input_year % 4 == 0 and input_year % 100 != 0) or \
(input_year % 400 == 0)
print(is_leap_year)