0% found this document useful (0 votes)
7 views

b.com python first unit

This document provides an introduction to algorithms, computer hardware, software, and programming concepts. It explains the roles of algorithms in problem-solving, the structure of computer hardware including CPUs and memory, and the function of operating systems. Additionally, it covers programming languages, syntax and semantics, program execution, and various types of operators and data types in Python.

Uploaded by

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

b.com python first unit

This document provides an introduction to algorithms, computer hardware, software, and programming concepts. It explains the roles of algorithms in problem-solving, the structure of computer hardware including CPUs and memory, and the function of operating systems. Additionally, it covers programming languages, syntax and semantics, program execution, and various types of operators and data types in Python.

Uploaded by

vivek.r00014
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT 1 : INTRODUCTION

What Is an Algorithm?

•An algorithm is a finite number of clearly described,


unambiguous “doable” steps that can be systematically
followed to produce a desired result for given input in a
finite amount of time

•Algorithms solve general problems and not specific ones


•An algorithm is a finite number of clearly
described, unambiguous “doable” steps that can be
systematically followed to produce a desired result
for given input in a finite amount of time.

•Because computers can execute instructions very quickly


and reliably without error,
algorithms and computers are a perfect match.
Computer Hardware

• Computer hardware comprises the physical part of a


computer system. It includes the all-important components
of the central processing unit (CPU) and main memory .

• It also includes peripheral components such as a


keyboard, monitor, mouse, and printer.

• Computer hardware, therefore, is based on the use of


simple
electronic “on/off” switches called transistors that switch
at very high speed. Integrated circuits (“chips”), the
building blocks of computer hardware, are comprised of
millions or even billions of transistors.

• All information within a computer system is represented


using only two digits, 0 and 1, called binary representation.

• We are used to representing numbers using base 10


with digits 0–9. Consider if information were represented
within a computer system

• In current electronic computing, each digit is


represented by a different voltage level. The more voltage
levels (digits) that the hardware must utilize and
distinguish, the more complex the hardware design
becomes. This results in greater chance of hardware
design errors.
• It is a fact of information theory, however, that any
information can be represented using only two symbols.

• Because of this, all information within a computer


system is represented by the use of only two digits, 0 and
1 , called binary representation , Base 2 has digits 0 and 1,
with place values that are powers of two, In this
representation, each digit can be one of only two possible
values, similar to a light switch that can be either on or off.

• Computer hardware, therefore, is based on the use


of simple electronic
“on/off” switches called transistors that switch at very high
speed.
Integrated circuits (“chips”), the building blocks of
computer hardware, are
comprised of millions or even billions of transistors.
The term bit stands for binary digit. A byte is a group of
bits operated on as a single unit in a computer system,
usually consisting of eight bits.
Fundamental Hardware Components

• The central processing unit (CPU) is the “brain” of


a computer system, containing digital logic circuitry able
to interpret and execute instructions.

• Main memory is where currently executing programs


reside, which the CPU can directly and very quickly access.

• Main memory is volatile; that is, the contents are


lost when the power is turned off.

• In contrast, secondary memory is nonvolatile, and


therefore provides long-term storage of programs and
data.

• This kind of storage, for example, can be magnetic


(hard drive), optical (CD or DVD), or nonvolatile flash
memory (such as in a USB)

Operating Systems

• Operating Systems—Bridging Software and Hardware An


operating system is software that has the job of managing
and interacting with the hardware resources of a
computer.
• Because an operating system is intrinsic to the
operation a computer, it is referred to as system software.
• An operating system acts as the “middle man” between
the hard ware and
executing application programs
• For example, it controls the allocation of memory for the
various programs that may be executing on a computer.

• perating systems also provide a particular user


interface. Thus, it is the operating system installed on
a given computer that determines the “look and feel”
of the user interface and how the user interacts with
the system, and not the particular model computer.
• An operating system is software that has the job of
managing the hardware resources of a given computer
and providing a particular user interface

What Is Computer Software?


• Computer software is a set of program instructions,
including related data and documentation, that can be
executed by computer.
• This can be in the form of instructions on paper, or in
digital form. While system software is intrinsic to a
computer system, application software fulfills users’ needs,
such as a photo- editing program.

• Computer software is a set of program


instructions, including related data and
documentation, that can be executed by computer.

What Are Syntax and Semantics?

• Programming languages (called “artificial


languages”) are languages just as “natural languages”
English.

• Syntax and semantics are important concepts that


apply to all languages. The syntax of a language is a set of
characters and the acceptable arrangements (sequences)
of those characters.

• English, for example, includes the letters of the


alphabet, punctuation, and properly spelled words and
properly punctuated sentences.

• “Hello there, how are you?”

• The following, however, is not syntactically correct,

• “Hello there, hao are you?”


In this sentence, the sequence of letters “hao” is not a
word in the English language.
Now consider the following sentence,

• “Colorless green ideas sleep furiously.”

This sentence is syntactically correct, but is semantically


incorrect, and thus has no meaning.

• The semantics of a language is the meaning


associated with each syntactically correct sequence
of characters.
• The syntax of a language is a set of characters
and the acceptable sequences of those characters.

• The semantics of a language is the meaning


associated with each syntactically correct sequence
of characters.

Program Translation

A central processing unit (CPU) is designed to interpret and


execute a specific set of instructions represented in binary
form (i.e., 1s and 0s) called machine code. Only programs
in machine code can be executed by a CPU,
Writing programs at this “low level” is tedious and error-
prone.

Therefore, most programs are written in a Execution


of Machine Code

Program Execution by Use of a Compiler


“high-level” programming language such as Python. Since
the instructions of such programs are not in machine code
that a CPU can execute, a translator program must be
used.

There are two fundamental types of translators. One,


called a compiler , translates programs directly into
machine code to be executed by the CPUThe other type of
translator is called an interpreter , which executes
program instructions in place of (“running on top of”)

Thus, an interpreter can immediately execute instructions


as they are entered. This is referred to as interactive
mode.
This is a very useful feature for program development.
Python, as we shall see, is executed by an interpreter.

• On the other hand, compiled programs generally


execute faster than interpreted programs. Any program
can be executed by either a compiler or an interpreter, as
long there exists the corresponding translator program for
the programming language that it is written in.

• A compiler is a translator program that translates


programs directly into machine code to be executed by the
CPU.

• An interpreter executes program instructions in place of


(“running on top of”) the CPU.

About Python

• Guido van Rossum (Figure 1-26) is the creator of


the Python programming language, fi rst released in the
early 1990s.

• Its name comes from a 1970s British comedy sketch


television show
called Monty Python’s Flying Circus. (Check them out on
YouTube!)

• The development environment IDLE provided with


Python comes from the name of a member of the comic
group

• The IDLE Python Development Environment IDLE is


an integrated development environment (IDE). An IDE is a
bundled set of software tools for program development.

• The Python Programming Language 23 an editor for


creating and modifying programs, a translator for executing
programs, and a program debugger. A debugger provides a
means of taking control of the execution of a program to aid
in finding program errors.

• Python is most commonly translated by use of an


interpreter. Thus, Python provides the very useful ability to
execute in interactive mode.
• The window that provides this interaction is referred to
as the Python shell .

• >>> shell prompt

What Is a Literal?
• To take something literally is to take it at “face value.”
The same is
true of literals in programming.
• A literal is a sequence of one of more characters that
stands for itself, such as the literal 12. We look at numeric
literals in Python next.
• A literal is a sequence of one or more characters
that stands for itself
• Numeric Literals
• String Literals
• Control Characters
• String Formatting

NUMERIC LITERALS
A numeric literal is a literal containing only the digits 0–9,
an optional sign character (1 or 2), and a possible decimal
point.

If a numeric literal contains a decimal point, then it


denotes a
Floating -point value, or “float” otherwise, it denotes an
integer value
A numeric literal is a literal containing only the
digits 0–9, a sign character (1 or 2) and a possible
decimal point. Commas are never used in numeric
literals

Ex:
STRING LITERALS
 Numerical values are not the only literal values in
programming. String literals, or “strings,” rep resent a
sequence of characters,

 In Python, string literals may be delimited (surrounded)


by a matching pair of either single (') or double (") quotes.

 >>> print ('Welcome to Python!')

 Output: Welcome to Python!

 A string consisting of only a pair of matching quotes


(with nothing in between) is called the empty string

 A string literal, or string, is a sequence of


characters denoted by a pair of matching single or
double (and sometimes triple) quotes in Python.

Variables and Identifiers

A variable is a name (identifier) that is associated with a


value, as for variable num d

A variable can be assigned different values during a


program’s execution—hence, the name “vari able.”
Wherever a variable appears in a program (except on the
left-hand side of an assignment statement)
What Is an Identifier?
 An identify er is a sequence of one or more characters
used to provide a name for a given program element.
Variable names line, Num credits, and gpa are each
identifiers.
 Python is case sensitive, thus, Line is different from line.
Identifiers may contain letters and digits, but cannot begin
with a digit. The underscore character, _, is also allowed to
aid in the readability of long identifier names.
 It should not be used as the fi r s t character, however, as
identifiers beginning with an underscore have special
meaning in Python.

Valid Identifiers Invalid Identifiers

totalSales ‘totalsales’ quotes not


allowed
totalsales total sales spaces not
allowed
salesFor2010 2010Sales cannot begin with a digit
sales_for_2010 _2010Sales should not begin an
underscore

An identifier is a sequence of one or more characters used


to name a given program element. In Python, an identifier
may contain letters and digits, but cannot begin with a
digit. The special underscore character can also be used

Keyword:

A keyword is an identifier that has predefined meaning in a


programming language and therefore cannot be used as a
“regular” identifier. Doing so will result in a syntax error

What Is an Expression?
 An expression is a combination of symbols that
evaluates to a value.
 Expressions, most commonly, consist of a
combination of operators and operands, 4 1 (3 * k) An
expression can also consist of a single literal or variable.
Thus, 4, 3, and k are each expression.
 This expression has two subexpressions, 4 and (3 *
k). Subexpression (3 * k) itself has two subexpressions, 3
and k.
 Expressions that evaluate to a numeric type are
called arithmetic expressions. A subexpression is any
expression that is part of a larger expression.

What Is an Operator?

An operator is a symbol that represents an operation that


may be performed on one or more operands. For example,
the 1 symbol represents the operation of addition.
+, - , * , / are usual operators

Ex:

20-5 =15 - is used a binary operator


-10*5= -50 - is used as a unary operator

 Python also includes an exponentiation (**)


operator. Integer and floating-point values can be
used in both the base and the exponent,
 2**4 ➝ 16
 2.5 ** 4.5 ➝ 61.76323555016366

 Python provides two forms of division. “true”


division is denoted by a single slash, /. Thus, 25 / 10
evaluates to 2.5.

 Truncating division is denoted by a double slash, //,


providing a truncated result based on the type of operands
applied to
 The modulus operator (%) gives the remainder of
the division of its operands

What Is a Data Type?


 A data type is a set of values, and a set of operators
that may be applied to those values.
 For example, the integer data type consists of the
set of integers, and operators for addition, subtraction,
multiplication, and division, among others.
 Integers, floats, and strings are part of a set of
predefined data types in Python called the built-in types.

 Finally, there are two approaches to data typing in


programming languages.
 In static typing, a variable is declared as a certain type
before it is used, and can only be assigned values of that
type. Python, however, uses dynamic typing.
 In dynamic typing, the data type of a variable depends
only on the type of value that the variable is currently
holding.
 Thus, the same variable may be assigned values of
different type during the execution of a program.

The operator can be defined as a symbol which is


responsible for a particular operation between two
operands. Operators are the pillars of a program on which
the logic is built in a specific programming language.
Python provides a variety of operators, which are described
as follows.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Bitwise Operators
o Membership Operators
o Identity Operators

Arithmetic Operators
Arithmetic operators are used to perform arithmetic
operations between two operands. It includes
+ (addition), - (subtraction), *(multiplication), /(divide), %
(reminder), //(floor division), and exponent (**) operators.

Operator Description

+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b
= 30
- (Subtraction) It is used to subtract the second operand from the first operand. If
the first operand is less than the second operand, the value
results negative. For

Comparison operator

Comparison operators are used to comparing the value of the two operands and returns Boolean
true or false accordingly. The comparison operators are described in the following table

Operator Description

== If the value of two operands is equal, then the condition becomes true.

!= If the value of two operands is not equal, then the condition becomes true.

<= If the first operand is less than or equal to the second operand, then the condition
becomes true.

>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.

> If the first operand is greater than the second operand, then the condition becomes
true.

< If the first operand is less than the second operand, then the condition becomes
true.

Assignment Operators

The assignment operators are used to assign the value of the right expression to the left operand. The
assignment operators are described in the following table.

Operator Description

= It assigns the value of the right expression to the left operand.

+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+
= b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10 => a-
= b will be equal to a = a- b and therefore, a = 10.

*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10, b
= 20 => a* = b will be equal to a = a* b and therefore, a = 200.

%= It divides the value of the left operand by the value of the right operand and
assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a
% = b will be equal to a = a % b and therefore, a = 0.

**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2
= 16 to a.

Bitwise Operators

The bitwise operators perform bit by bit operation on the values of the two operands. Consider the
following example.
For example,
if a = 7
b=6
then, binary (a) = 0111
binary (b) = 0110

hence, a & b = 0011


a | b = 0111
a ^ b = 0100
~ a = 1000

Operator Description

& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the
result. Otherwise, 0 is copied.

| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit
will be 1.

^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting
bit will be 0.

~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the

resulting bit will be 1 and vice versa.

<< (left shift) The left operand value is moved left by the number of bits present in the right
operand.
>> (right shift) The left operand is moved right by the number of bits present in the right
operand.

Logical Operators

The logical operators are used primarily in the expression


evaluation to make a decision. Python supports the following
logical operators.

Operator Description

and If both the expression are true, then the condition will be true. If a and b
are the two expressions, a → true, b → true => a and b → true.

or If one of the expressions is true, then the condition will be true. If a and b
are the two expressions, a → true, b → false => a or b → true.

not If an expression a is true, then not (a) will be false and vice versa.

Membership Operators

Python membership operators are used to check the membership of


value inside a Python data structure. If the value is present in the data
structure, then the resulting value is true otherwise it returns false.
Operator Description

in It is evaluated to be true if the first operand is found in the second operand (list,
tuple, or dictionary).

not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).
Identity Operators

The identity operators are used to decide whether an element certain class or type.

Operator Description

is It is evaluated to be true if the reference present at both sides point to the


same object.

is not It is evaluated to be true if the reference present at both sides do not point
to the same object.

Operator Precedence

The precedence of the operators is essential to find out since it enables us to know which
operator should be evaluated first. The precedence table of the operators in Python is given
below.

Operator Description

** The exponent operator is given priority over all the others used in the
expression.

~+- The negation, unary plus, and minus.

* / % // The multiplication, divide, modules, reminder, and floor division.

+- Binary plus, and minus

>> << Left shift. and right shift

You might also like