0% found this document useful (0 votes)
12 views25 pages

Introduction to Hardware, Software, and Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views25 pages

Introduction to Hardware, Software, and Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter: 0

Prerequisite: Understanding Hardware, Software, and Programming

1. Introduction to Hardware and Software

To understand computers, it's essential to know about two critical components: hardware and
software.
● Hardware: These are the physical parts of a computer that you can touch.
○ Think of the computer's monitor, keyboard, mouse, and the components inside
the computer case like the CPU (Central Processing Unit), RAM (Random Access
Memory), and hard drive.
○ Hardware is like the body of a computer; it provides the structure and the parts
needed to do tasks.
Note: When we says Memory it means we are talking about the RAM

● Software: This is the set of instructions that tells the hardware what to do.
○ Software is intangible—you can't touch it.
○ Examples include operating systems (like Windows or macOS),
○ Applications (like Microsoft Word or Google Chrome), and games.
○ Software is like the brain of the computer; it makes decisions and processes tasks
using the hardware.

2. What is Programming?

● Programming is the process of giving instruction to machines that will lead to creating
software.
● It's like writing a recipe for the computer to follow.
● Just as a recipe has steps for making a dish, a program has steps that the computer must
follow to perform a task.

Why do we need programming?:


Computers are incredibly fast and can perform millions of calculations per second. But
they are not smart—they only do what they are told.
Programming is needed to instruct computers to carry out specific tasks, from simple
things like adding numbers to complex ones like running a video game.
The role of a programmer:
A programmer writes the code (the instructions) that tells the computer what to do. This
code is written in a programming language, which is like a language the computer understands.

3. Types of Programming Languages

Programming languages are tools that allow us to write instructions for computers.
There are many programming languages, each with its own uses. Here's a breakdown of the
main types:

Low-Level Languages:
● Machine Language: This is the language that a computer's hardware understands
directly. It's made up of binary code (1s and 0s). Writing programs in machine language
is very difficult, so it's rarely done by humans.
● Assembly Language: A small step above machine language, assembly language uses
short codes or mnemonics to represent machine instructions (e.g., `MOV`, `ADD`).
Assembly language is still quite complex but easier to understand than pure machine
language.

High-Level Languages:
● Procedural Languages: These languages, like C and Pascal, are designed to follow a
sequence of steps or procedures. They focus on the “how” of the problem-solving
process.
● Object-Oriented Languages: These languages, like Java and Python, use objects (which
bundle data and methods) to design software. They focus on modeling real-world things
as objects in the code.
● Scripting Languages: Languages like JavaScript, Python, and Ruby are often used to
automate tasks. They are usually easier to learn and are commonly used in web
development.
● Functional Languages: Languages like Haskell and Lisp (Name of languages) are designed
to handle complex math problems and focus on using functions to solve tasks. They are
best used when a lot of complicated calculations are required.

4. Compiler vs. Interpreter vs. Assembler

When you write a program, it's usually in a high-level language. But the computer only
understands machine language. To bridge this gap, we use compilers, interpreters, and
assemblers.

Compiler:
A compiler translates a high-level language (like C or Java) into machine language all at
once, creating an executable file (like `.exe`). This file can then be run on the computer. The
translation happens before the program is run, so the program runs faster after being compiled.

Example: If you write a program in C, the compiler will turn your C code into machine
code that your computer can execute.

Interpreter:
An interpreter also translates high-level language into machine language, but it does so
line by line, as the program runs.
This means that the program doesn't need to be compiled before running, but it can be
slower because the translation happens during execution.

Example: Python uses an interpreter. When you run a Python script, the interpreter
reads and executes the code line by line.

Assembler:
An assembly language which is basically mnemonics like GO, HALT, JUMP, and NOT code
which is translated to the machine language by programming language translator i.e.,
Assembler. is a program that takes assembly language as source code and converts it into the
bit format i.e machine language which is understandable by the computers. For example NASM
and MASM.
Chapter 1
Introduction to Python Programming

Python is a popular programming language known for being easy to read and write. It's
used by beginners and professionals alike to create websites, automate tasks, analyze data, and
much more. Python is powerful because it has many built-in tools and a large community that
shares useful libraries and resources. Whether you're new to programming or have some
experience, Python is a great language to learn because of its simplicity and versatility.

1.1 History of Python

● How did it begin?


Like C, C++, Java, Python came from a research background where the
programmer was having a hard time getting the job done with the existing tools.
● Guido van Rossum has used best features of all languages available like:
Functional programing feature from C
OOP features from C++
Scripting language features from pearl and Shell Script
Modular programming features from modula 3
Syntax from C and ABC language

Using all these features from different languages he developed Python.

Python is a high-level, interpreted programming language created by Guido van Rossum


and first released in 1991. The development of Python began in the Late 1980s, at Centrum
Wiskunde & Informatica (CWI) in the Netherlands, and its implementation started in December
1989. Guido van Rossum wanted to create a language that was easy to read and simple to
understand, which would bridge the gap between the C programming language and shell
scripting.

Python was named after the British comedy group "Monty Python's Flying Circus" which
Guido van Rossum enjoyed watching.
He wanted the language to be fun, in addition to being easy to use.

Over the years, Python has evolved, with Python 2.0 released in 2000, which introduced
many new features like list comprehensions and garbage collection.
In 2008, Python 3.0 was released, which was not backward compatible with Python 2.x
versions. Python 3 introduced many improvements in terms of code readability and consistency,
although the transition from Python 2 to Python 3 took time as developers needed to update
their existing codebases.

1.2 Need for Python / Why Python?

Python was developed to meet the need for a versatile, easy-to-learn, and easy-to-use
programming language that could handle various tasks across different domains. The primary
reasons for the growing popularity of Python include:

1. Ease of Learning: Python’s syntax is clean and readable, which makes it accessible to
beginners. Its design philosophy emphasizes code readability and simplicity.

2. Wide Applicability: Python is used in a variety of fields, including web development, data
science, artificial intelligence, automation, scientific computing, and more. It is often the first
choice for prototyping, and due to its simplicity, many educational institutions use it for
teaching programming.

3. Extensive Libraries and Frameworks: Python comes with a rich set of standard libraries and
frameworks like NumPy, Pandas, Django, Flask, TensorFlow, and PyTorch, which accelerate
development in various domains.

4. Quick Prototyping: Python runs on an interpreter system, meaning that code can be executed
as soon as it is written line by line. This means that prototyping can be very quick.

4. Community Support: Python has a large, active community, which contributes to a wealth of
resources, tutorials, and third-party modules. This vibrant ecosystem makes it easier to find help
and collaborate with others.
1.3 Features of Python

Python’s design and functionality offer several features that make it stand out among
programming languages:

1. Simple and Easy to Learn: Python’s syntax is clear, concise, and easy to understand. This
simplicity makes it ideal for beginners and for rapid development.

2. Interpreted Language: Python is an interpreted language, meaning that code is executed line
by line, which makes debugging easier and also fast prototyping.

3. Platform-Independent: Python is cross-platform, meaning that it can run on various


operating systems like Windows, macOS, and Linux without requiring changes to the code.

4. Extensive Standard Library: Python’s standard library includes modules for various tasks like
regular expressions, file I/O, web development, and more, reducing the need for writing code
from scratch.

5. Procedure and Object-Oriented: Python is a procedure oriented as well as object oriented


programming [Link] procedure oriented programming languages, the programs are built
using functions and procedures.
But in object oriented programming languages the program uses classes and
[Link] supports object-oriented programming (OOP), allowing developers to create
reusable code through the use of classes and objects.

6. Dynamic Typing: Python is dynamically typed, meaning that the type of a variable is
determined at runtime. This flexibility allows for faster development.

7. High-Level Language: Python abstracts away much of the complexity involved in tasks like
memory management, making it easier to focus on writing code rather than managing
resources.

8. Extensible and Embeddable: Python can be extended with modules written in other
languages like C or C++, and it can be embedded into applications written in these languages.

9. Open Source: There is no need to pay for Python. Python can be freely downloaded from
[Link] website. Its source codes can be read, modified and can be used in programs
as desired by the programmers.
10. Database connectivity: A database represents software that stores and manipulates data.
For example, Oracle is a popular database using which we can store data in the form of
tables and manipulate the data.
Python provides interfaces to connect its programs to all major databases like Oracle, Sybase or
MySql.

11. Good Memory Management: Python runs garbage collector mechanism automatically
hence utilizes memory effectively.

1.4 Comparison with C and Java

Feature Python C Java

Type System Dynamically typed Statically typed Statically typed

Syntax Simple and readable Complex, less readable Verbose, more


structured

Memory Automatic (Garbage Manual (Pointers) Automatic (Garbage


Management Collection) Collection)

Compilation Interpreted Compiled Compiled (JVM


Bytecode)

Performance Slower (due to Faster (direct to Moderate (Bytecode to


interpretation) machine code) machine code)

Portability Highly portable Less portable (needs Highly portable (JVM)


recompilation)

Usage Web development, System programming, Enterprise applications,


data science embedded Android
C Vs. Python

C Language Python

C language does not contain the features like Python is an object-oriented language. It contains
classes, objects, inheritance, polymorphism, features like classes, objects, inheritance,
etc. polymorphism, etc.

C programs execute faster. Python programs are slower compared to C. PyPy


flavor of Python programs run a bit faster but still
slower than C language.

It is compulsory to declare the data types of Type declaration is not required in Python.
variables, arrays, etc in C language.

C language is statically typed. Python is dynamically typed.

Pointers are available in C language. Python does not use pointers.

C language does not directly support exception Python handles exceptions and hence Python
handling. programs are robust.

C language has for, while and do_while loops. Python has for and while loops only. It does not
have a do_while loop.

The variable in for loop does not increment The variable in the for loop increments
automatically. automatically.

C language has switch statements. Python does not have a switch statement.

The programmer should allocate and deallocate Memory allocation and deallocation is done
memory using malloc(), calloc(), realloc(), or free automatically by PVM.
functions.
C does not contain a garbage collector. Automatic garbage collector is available in Python.

C supports single and multi-dimensional arrays. Python supports only single dimensional arrays.
Third party applications like numpy should be used
to work with multidimensional arrays.
The array index should be a positive integer. Array index can be positive or negative integer
numbers.
Indentation of statements is not necessary in C. Indentation is required to represent a block of
statements.
A semicolon is used to terminate and a comma New line indicates end of the statement and
is used to separate expressions. semicolon is used as an expression separator.
C is typically used for system programming, Python is used in web development, scripting, data
embedded systems, and applications requiring analysis, and automation.
high performance

JAVA Vs Python

JAVA Language Python

Java is an object-oriented programming Python blends functional programming with object


language. Functional programming features are oriented programming features. Lambdas are
introduced into Java 8.0 through lambda already available in Python.
expressions.

Java programs require more lines. Python programs are short and compact. A big
problem can be written using very less no. of lines.

It is compulsory to declare the data types of Data type declaration is not required in Python.
variables, arrays, etc in Java.

Java language is statically typed. Python is dynamically typed.

JAVA language has do_while, while, for loops. Python has for and while loops only.

JAVA has switch statement Python Does not have Switch Statement

The variable in for loop does not increment The variable in the for loop increments
automatically. automatically.

Memory allocation and deallocation is done Memory allocation and deallocation is done
automatically by JVM (Java Virtual Machine). automatically by PVM (Python Virtual Machine).

Java supports single and multi-dimensional Python supports only single dimensional arrays. To
arrays. work with multi- dimensional arrays, third party
applications like numpy are used.

An array index should be a positive integer. Array index can be a positive or negative integer
number. Negative index represents locations from
the end of the array.
Indentation of statements is not necessary in Indentation is required to represent a block of
JAVA language. statements.
A semicolon is used to terminate the New line indicates end of statement and semicolon
statements and a comma is used to separate is used as an expression separator.
expressions.
Java is widely used in enterprise applications, Python is preferred for web development, data
Android development, and large systems. science, and scripting.

1.5 Python Building Blocks

Understanding the fundamental building blocks of Python is essential for writing efficient and
effective code. These building blocks include keywords, identifiers, variables, comments,
docstrings, indentation, and input-output operations.

1.5.1 Keywords

● Keywords in Python are reserved words that have a special meaning and purpose in the
language.
● They cannot be used as identifiers (names of variables, functions, etc.). Python has a set
of predefined keywords, such as `if`, `else`, `while`, `for`, `def`, `return`, and many more.
● All Reserved words in Python contain only alphabet
● All Keywords are lower case, except the following three
● True
● False
● None
● You can see the list of all keywords in Python by using the `keyword` module:

import keyword
print([Link])
1.5.2 Identifiers

Identifiers are the names given to variables, functions, classes, and other objects in
Python. They must follow specific rules:
- Identifiers can only contain letters (uppercase (A-Z) or lowercase (a-z)), digits(0-9), and
underscores (_).
- They must start with a letter or an underscore, not with a digit.
- Identifiers are case-sensitive (`Variable` and `variable` are different).
- Certain names are reserved and cannot be used as identifiers because they conflict with
Python’s keywords.
Eg.: def = 10
Not valid in python as it is keyword

Which one will be correct?


Case 1: Cash = 100
Case 2: Ca$h = 100

In case 1 variable is valid as it start with letter and not contains any symbol
1.5.3 Variables

● Variables are containers for storing data values. In Python, you don’t need to declare the
data type of a variable explicitly; the interpreter infers it based on the value assigned:

● If we type x = 10 then automatically python interpreter will decides data types of x as


“int” no need of prior declaration like C language

In Python langauage declaring the variable In C language Declaring the variable

x = 10 int x ;
x = 10 ;

● If we typed y = "Hello" then y data type will be string

● Python allows multiple variables to be assigned in a single line:


a, b, c = 1, 2, "Python"
In the above case 1 value will be assigned to a, 2 value will be assigned to b and Python
value will be assigned to c variable.
● Variables can even change type after they have been set.
x=4
x = “Hello”
print(x)
In the above case the output will be Hello.

1.5.4 Comments

● Comments in Python are non-executable statements that are used to explain code and
make it more understandable.
● They are ignored by the interpreter and do not affect the execution of the program.

● Python supports two types of comments:


○ Single-line comments: Begin with a `#` symbol.
# This is a single-line comment
○ Multi-line comments: Although Python doesn’t have a specific syntax for
multi-line comments, you can use triple quotes (""" or ''') to create block
comments.
"""
This is a multi-line comment
spanning across multiple lines.
"""

1.5.5 Docstring

● A docstring is a special type of comment


● It is used to describe the purpose of a function, class, or module.

● Declaring Docstrings: The docstrings are declared using ”’triple single quotes”’ or “””
triple double quotes “”” just below the class, method, or function declaration. All
functions should have a docstring.
Following in an example of declaring docstring
def my_function():
"""
This is a docstring. It explains the purpose of the function.
This function prints Hello world
"""
print("Hello, World!")

● Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the
object OR using the help function. The below examples demonstrate how to declare
and access a docstring.

Method 1

def my_function():
'''Demonstrates triple double quotes
docstrings and does nothing really.
It tells what function does '''

print(“Hello word”)
print("Using __doc__:")
print(my_function.__doc__)

Using __doc__:
Demonstrates triple double quotes
docstrings and does nothing really.
It tells what function does

Method 2

def my_function():
'''Demonstrates triple double quotes
docstrings and does nothing really.
It tells what function does '''

print(“Hello word”)

print("Using help:")
help(my_function)

Using help:
Help on function my_function in module __main__:

my_function()
Demonstrates triple double quotes
docstrings and does nothing really.
It tells what function does

1.5.6 Indentation

● Indentation refers to the spaces or tabs at the beginning of a line. Unlike many other
programming languages that use braces `{}` to define code blocks,
● Python uses indentation to determine the grouping of statements.
● Proper indentation is crucial in Python, as it defines the structure and flow of the code:
if x > 0:
print("x is positive") # This is Block of code
else:
print("x is non-positive") # This is Block of code

● All statements within a block must be indented by the same amount.


● Incorrect indentation will result in an `IndentationError`.

1.5.7 Input-Output

● Python provides built-in functions for input and output operations:

● Input: The `input()` function is used to take input from the user.
○ It always returns the input as a string.
● >>> name = input("Enter your name: ")
Above instruction will accept the data from the user through the keyboard and will
assign that value to the variable name. Default data type of name will be string only.
This way of writing input instructions is always recommended.
● >>> input(“ENter your name”)
Above instruction will accept the value from the user through the keyboard but the
value will be stored in the memory which we can not use easily further as we do not
have a memory tag for the same. Hence this method is not recommended.

● Output: The `print()` function is used to display output to the console.


print() # Prints Blank line
print("Hello") # Prints Hello
print("Hello"*3) # Prints Hello thrice
print("Welcome to " + "CSMSS") # Prints bothe strings in joined format
print(20) # Prints 20
a=5.5 # Assign 5.5 to a variable
print(a) # Prints the value of tag a or variable a
print("The value of a is = ", a) # Prints the given string and then value in same line
print (a+ "Hello") # Will show the error

Hello
HelloHelloHello
Welcome to CSMSS
20
5.5
The value of a is = 5.5

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[6], line 9
7 print(a) # Prints the value of tag a or variable a
8 print("The value of a is = ", a) # Prints the given string and then value in same line
----> 9 print (a+ "Hello") # Will show the error

TypeError: unsupported operand type(s) for +: 'float' and 'str'

Let us do some program:


Note: In exam for every program one must write answer in following format:
1. Write Algorithm
2. Draw a flowchart
3. Write Python code
4. Write the output of program

1. Write a program to read 2 numbers from the keyboard and print sum.
Algorithm:
Step 1: Start the program
Step 2 : Read the two input from user through the keyboard
Step 3: Calculate the Sum
Step 4: Print the result on console
Step 5: Stop the program

Flowchart:

Python Code:
Output:
Homework:

1. Sum of five Numbers: Write a program that takes five numbers as input and outputs
their sum.
2. Product of Two Numbers: Write a program that takes two numbers as input and outputs
their product.
3. Difference Between Two Numbers: Write a program that takes two numbers as input
and outputs the difference between the first and the second number.
4. Square of a Number: Write a program that takes a number as input and outputs its
square.
5. Cube of a Number: Write a program that takes a number as input and outputs its cube.
6. Average of Three Numbers: Write a program that takes three numbers as input and
outputs their average.
7. Perimeter of a Rectangle: Write a program that takes the length and width of a rectangle
as input and outputs its perimeter.
8. Area of a Rectangle: Write a program that takes the length and width of a rectangle as
input and outputs its area.
9. Area of a Triangle: Write a program that takes the base and height of a triangle as input
and outputs its area.
10. Circumference of a Circle: Write a program that takes the radius of a circle as input and
outputs its circumference.
11. Area of a Circle: Write a program that takes the radius of a circle as input and outputs its
area.
12. Convert Celsius to Fahrenheit: Write a program that takes a temperature in Celsius as
input and outputs the temperature in Fahrenheit.
13. Convert Fahrenheit to Celsius: Write a program that takes a temperature in Fahrenheit
as input and outputs the temperature in Celsius.
14. Simple Interest Calculation: Write a program that takes the principal, rate, and time as
input and outputs the simple interest.
15. Compound Interest Calculation: Write a program that takes the principal, rate, and time
as input and outputs the compound interest.
16. Pythagorean Theorem: Write a program that takes the lengths of the two legs of a right
triangle as input and outputs the length of the hypotenuse.
17. Quadratic Equation Roots: Write a program that takes the coefficients of a quadratic
equation as input and outputs the roots of the equation.
18. Number of Seconds in a Day: Write a program that takes a number of days as input and
outputs the total number of seconds.
19. Percentage Calculation: Write a program that takes the marks obtained and total marks
as input and outputs the percentage.
20. Distance Between Two Points: Write a program that takes the coordinates of two points
in a plane as input and outputs the distance between them.
21. BMI Calculation: Write a program that takes the weight and height of a person as input
and outputs their Body Mass Index (BMI).
22. Speed Calculation: Write a program that takes distance and time as input and outputs
the speed.
23. Currency Conversion: Write a program that takes an amount in one currency as input
and outputs the equivalent amount in another currency using a given exchange rate.
24. Time Conversion: Write a program that takes a time in hours and minutes as input and
outputs the total time in minutes.
25. Power Calculation: Write a program that takes a base number and an exponent as input
and outputs the result of raising the base to the exponent.
26. The distance between two cities (in km.) is input through the keyboard. Write a Python
program to convert and print this distance in meters and centimeters.

These problems are great for practicing basic input, output, and arithmetic operations in
Python.

1.6. Installing Python for Windows:

What Is Python IDLE?


Every Python installation comes with an Integrated Development and Learning
Environment, which you’ll see shortened to IDLE or even IDE.
These are a class of applications that help you write code more efficiently. While there are many
IDEs available in the market.

For installing Python IDLE on windows following steps need to be followed.


1. Download the Python installer from official website
2. Run the installer
3. Install Python
4. Verify the installation

Step 1: Download the Python installer from official website:


Visit the official website of Python that is [Link] and click on the
“Download” option.

Select the Operating System (WIndows / Mac Os )


Select the Python version as per your need. As shown in pic
Note that Python 3.12.5 cannot be used on Windows 7 or earlier.

Step 2: Run Python Installer:


Locate the downloaded installer file (usually in your Downloads folder) and
double-click on it to run the installation process.
You may be prompted by the User Account Control (UAC) to allow the
installation. Click Yes to proceed.
Step 3: Install the python
Select your desired installation settings
Click Install to begin the installation process. The installer will copy the necessary
files to your computer and set up Python. This process may take a few minutes.
At the end it will show installation is successful.
Step 4: Verify installation

Once the installation is complete, you can verify that Python has been installed
correctly by opening the Command Prompt (search for “cmd” in the Start menu)
and typing the following command:
python - - version
Press Enter, and you should see the version of Python you installed displayed in
the output. This confirms that Python has been successfully installed on your
computer.
For installation refer this links:
a. Installing Jupyter on Windows

[Link]

b. Installing VS code on Windows


[Link]
c. Installing Pycharm on Windows
[Link]

d. Installing Spyder on Windows


[Link]

In this Unit, we explored the basics of Python, starting with its history and the reasons
behind its creation and widespread adoption. We also examined Python's features and how it
compares with other popular programming languages like C and Java. Finally, we covered the
foundational building blocks of Python, including keywords, identifiers, variables, comments,
docstrings, indentation, and input-output operations. Understanding these basics is essential for
anyone starting their journey in Python [Link] the end we have seen how to install
Python various IDEs on windows operating systems.

Refer the book:


1. “Core Python Programming” by Dr. R. Nageswara Rao, Dreamtech Press.

You might also like