Week 6 First Year
Week 6 First Year
What is a program?
A program is a sequence of instructions that specifies how to perform a computation.
The computation might be something mathematical, such as solving a system of
equations or finding the roots of a polynomial, but it can also be a symbolic computation,
such as searching and replacing text in a document.
Programming languages are formal languages that have been designed to express
computations. The details look different in different languages, but a few basic
instructions appear in just about every language. we can describe programming as the
process of breaking a large, complex task into smaller and smaller subtasks until the
subtasks are simple enough to be performed with basic instructions.
We can choose any language for writing a program according to the need. But a
computer executes programs only after they are represented internally in binary form
(sequences of 1s and 0s). Programs written in any other language must be translated to
the binary representation of the instruction before the computer can execute them.
Programs written for a computer may be in one of the following categories of
languages:
• Machine Language: a sequence of instructions written in the form of binary numbers
consisting of 1s, 0s to which the computer responds directly
• Assembly Language: consists of a set of symbols and letters and requires translation to
machine language
• High-level Language: is a programming language that uses English and mathematical
symbols in its instructions.
1 29
Elements of a Programming Language
variables are the smallest components of a programming language. we must learn how
to use the internal memory of a computer in writing a program. Computer memory is
divided into several locations. Each location has got its own address.
Python variables do not need explicit declaration to reserve memory space. variable is
created automatically when you assign a value to it. The equal sign (=) is used to assign
values to variables.
The operand to the left of the = operator is the name of the variable and the operand to
the right of the = operator is the value stored in the variable. For example –
2 30
You can also assign multiple objects to multiple variables. For example –
Variable names
Every variable should have a unique name like a, b, c. A variable name can be
meaningful like color, age, name etc. There are certain rules which should be taken
care while naming a Python variable:
Keywords define the language’s rules and structure, they cannot be used as variable
names. Python has twenty-nine keywords:
3 31
Example: Following are valid Python variable names:
4 32
2. Data Types:
Data Types are used to define the type of a variable. It defines what type of data
are going to store in a variable. The data stored in memory can be of several types. For
example, a person's age is stored as a numeric value and his or her address is stored as
alphanumeric characters.
2.1 Boolean Data Types
Boolean type is one of built-in data types which represents one of the two values
either True or False. bool() function allows to evaluate the value of any expression and
returns either True or False based on the expression.
Examples: Following is a program which prints the value of boolean variables a and b
Following is another program which evaluates the expressions and prints the return
values:
5 33
This produce the following result
var2 = 10
var3 = 10.023
a=100
# float variable.
b=20.345
# complex variable.
c=10+3j
6 34
2.3 String Data Type
Strings are identified as a contiguous set of characters represented in the quotation
marks. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes
starting at 0 in the beginning of the string and working their way from -1 at the end.
. For example: -
str = 'Hello World!'
7 35
This produce the following result:-
['abcd', 786, 2.23, 'john', 70.2]
abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
3. Operators
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
8 36
Example: Following is an example which shows all the above operations:
a = 21
b = 10
# Addition
print ("a + b : ", a + b)
# Subtraction
print ("a - b : ", a - b)
# Multiplication
print ("a * b : ", a * b)
# Division
print ("a / b : ", a / b)
# Modulus
print ("a % b : ", a % b)
# Exponent
print ("a ** b : ", a ** b)
# Floor Division
print ("a // b : ", a // b)
Comparison operators compare the values on either sides of them and decide the
relation among them Operator Name Example
9 37
4.Input and Output
Developers often have a need to interact with users, either to get data or to provide some
sort of result. Most programs today use a dialog box as a way of asking the user to
provide some type of input.
input ( prompt )
This function first takes the input from the user and converts it into a string. Python
provides a built-in function called input which takes the input from the user.
When the input function is called it stops the program and waits for the user’s input.
When the user presses enter, the program resumes and returns what the user typed.
Example:
# Program to check input
# type in Python
num = input ("Enter number :")
print(num)
name1 = input("Enter name : ")
print(name1)
# Printing type of input value
print ("type of number", type(num))
print ("type of name", type(name1))
Output
In Python, we can simply use the print() function to print output. It allows us to add
specific values:
• object - value(s) to be printed
• sep (optional) - allows us to separate multiple objects inside print().
• end (optional) - allows us to add specific values like new line "\n", tab "\t"
print('Good Morning! \n')
print('It is rainy today')
print('New Year', 2023, 'See you soon!', sep= '. ')
1041
5. Comments
As programs get bigger and more complicated, they get more difficult to read. Formal
languages are dense, and it is often difficult to look at a piece of code and figure out
what it is doing, or why. For this reason, it is a good idea to add notes to your programs
to explain in natural language what the program is doing. These notes are called
comments, and they are marked with the # symbol:
Example: Following is an example of a single line comment
# This is a single line comment in python
print ("Hello, World!")
1142