COMPUTER PROGRAMMING
Computer Programming:
Computer programming is the process of writing instructions that tell a computer how to perform a
specific task. These instructions are written in a programming language such as Python.
Steps:
1. Write Code: Writing instructions in a programming language.
2. Compile/Interpret: Converting the code into machine-understandable form.
3. Execute: Running the program to perform the task.
4. Output: Displaying the result of the program.
Python:
Python is one of the most popular and widely used programming languages today. It is a versatile
language used in various fields such as web development, data analysis, and artificial intelligence.
Python's simple syntax and clear structure make it easier to understand programming concepts.
Comments:
Comments are the lines of text in a program that are not executed by the Python interpreter. They are used
to provide explanations or notes for the code, so that other programmers can understand the code more
easily.
Types of Comments:
1. Single-line comments
2. Multi-line comments
Variable:
A variable is a named memory location. It is used to store program's input data. The value of a variable
may change during the execution of a program. However, the name of a variable cannot be changed. If a
new value is stored in a variable, it replaces the previous value. Each value has a unique name called an
identifier.
Rules for Variable Name:
1. The first character of a variable name must be an alphabet or underscore.
2. The characters allowed in a variable name include uppercase letters, lowercase letters, digits, and
underscores.
3. Python is a case-sensitive language (uppercase and lowercase letters are different).
4. Special symbols are not allowed in a variable name.
5. Spaces are not allowed in a variable name.
Data Type:
A data type indicates the type of data and the set of operations that can be performed on that data.
Basic Types:
1. Integer
2. Floating point
3. String
4. Boolean
Input Function:
The input function in Python is used to accept input from the user during program execution.
Syntax:
variable = input("prompt")
Output Function:
The output function in Python is used to display the result of the program. The output is mostly displayed
on the monitor. The output displayed on the monitor is known as standard output.
int Function:
The int function is used to handle integer inputs. It converts the input value to an integer data type.
Syntax:
variable = int(input("prompt"))
float Function:
The float function is used to handle float input. It converts the input value to a float data type.
Syntax:
variable = float(input("prompt"))
Operators:
Operators are the symbols used to perform various operations on data in a program. Python provides
different varieties of operators such as:
1. Arithmetic operators
2. Assignment operators
3. Arithmetic assignment operators
4. Comparison operators
5. Logical operators
Assignment Operator:
The assignment operator is used to assign a value to a variable. In Python, the equal sign (=) is used as the
assignment operator. A statement that assigns a value to a variable is called an assignment statement. The
variable name is written on the left side, and the value or expression is written on the right side.
Syntax:
variable = expression
Arithmetic Operators:
An arithmetic operator is a symbol that performs mathematical operations on data. Python provides
arithmetic operations such as addition, subtraction, multiplication, division, floor division, modulus, and
exponent. The modulus operator is also called the remainder operator and works only with integer values.
Operator Precedence:
The order in which different operators in an expression are evaluated is called operator precedence. It is
also known as the hierarchy of operators.
Hierarchy of Operators:
1. Parentheses
2. Exponentiation
3. Multiplication, Division, Remainder
4. Addition, Subtraction
Comparison Operator:
A comparison operator is used to compare two variables and produces a result in the form of True or
False. These are also called relational operators.
Types:
• Greater than (>)
• Less than (<)
• Equal to (==)
• Greater than or equal to (>=)
• Less than or equal to (<=)
• Not equal to (!=)
Logical Operators:
Logical operators are used to combine multiple conditions or reverse a condition. There are three logical
operators in Python:
1. AND: returns True only if both conditions are True.
2. OR: returns True if at least one condition is True.
3. NOT: reverses the result of a condition.
Decision Making:
Decision making structures are used to execute statements based on a condition. The statement runs if the
condition is True and is skipped if the condition is False. It is also called a selection or conditional
structure.
Common decision-making structures in Python:
• IF
• IF-ELSE
• IF-ELIF-ELSE
IF Statement:
The IF statement is the simplest form of conditional statement. If the condition is True, the statement is
executed. If the condition is False, the statement is not executed.
Syntax:
if condition:
statement
IF-ELSE Statement:
The IF-ELSE statement is used for two-way decisions. It executes one block when the condition is True
and another block when it is False.
Syntax:
if condition:
statement
else:
statement
Short-hand IF-ELSE Statement:
The short-hand IF-ELSE statement in Python is a single-line expression used for simple conditions.
Syntax:
value_if_true if condition else value_if_false
IF-ELIF-ELSE Statement:
The IF-ELIF-ELSE statement is used to choose one block of statements from many blocks based on
different conditions.
Syntax:
if condition:
statement
elif condition:
statement
else:
statement
While Loop:
The while loop executes one or more statements as long as the given condition is True. It checks the
condition before each iteration and stops when the condition becomes False. It is useful when the number
of iterations is not known in advance.
Syntax:
while condition:
statement
For Loop:
The for loop executes statements for a specified number of times. It is also called a counter loop.
Syntax:
for variable in values:
statement
Module:
A module is a file that defines functions, classes, and variables. It may also include executable code. A
module file is saved with a (.py) extension and can be imported into other programs using the import
statement.
Function:
A function is a block of code that performs a specific task. The statements inside a function are executed
when it is called by its name. A function can be called many times to perform the same task.
Defining a Function:
Each user-defined function must be defined before it is used in the program.
Syntax:
def fname(parameters):
statement
Function Call:
A function call is a statement that invokes a function. A function runs when it is called by its name
followed by parentheses.
Parameter:
A parameter is the data passed into a function when it is called. The function can use its parameters in
calculations or other operations.
Default Parameters:
Default parameters are values assigned to parameters while defining a function. If no value is passed
during the function call, the default value is used.
Syntax:
def function_name(parameter = default_value):
statement
Data Structures:
A data structure is a container used to store data items in a specific layout. Python provides built-in data
structures that help in organizing and managing data efficiently. The most commonly used data structures
are List and Tuple.
List:
A list is a data structure in Python that can store multiple values. A list is mutable, which means its
elements can be created, accessed, modified, or removed easily. Each item stored in a list is called an
element. Lists can store elements of different data types such as integers, floats, and strings. Each element
in a list is accessed by its position, which is called an index. Each element has a unique index.
Purpose of Lists:
Lists are used to store and organize a large collection of data items easily. Basic variables can store only
one value at a time, but a list can store many values together.
Tuple:
A tuple is a data structure in Python that can store multiple values. A tuple is immutable, which means its
elements cannot be modified after creation. Each item in a tuple is called an element. Tuples can store
different data types such as integers, floats, and strings. Each element in a tuple is accessed by its position
called an index. Each element has a unique index.
Syntax:
tuple_name = (value1, value2, valueN)
Modular Programming:
Modular programming is a technique used to divide a program into small, manageable, and reusable parts
called modules. It makes the code easier to read, test, and maintain. Modules can be reused in different
programs, which reduces code repetition.