UNIT-1 WHAT IS COMPUTER SCIENCE? Computer Science is the study of computers and computational systems. Computer scientists deal mostly with software and software systems; this includes their theory, design, development, and application. Principal areas of study within Computer Science include artificial intelligence, computer systems and networks, security, database systems, human computer interaction, vision and graphics, numerical analysis, programming languages, software engineering, bioinformatics and theory of computing. Although knowing how to program is essential to the study of computer science, it is only one element of the field. Computer scientists design and analyze algorithms to solve programs and study the performance of computer hardware and software. The problems that computer scientists encounter range from the abstract-- determining what problems can be solved with computers and the complexity of the algorithms that solve them – to the tangible – designing applications that perform well on handheld devices, that are easy to use, and that uphold security measures. COMPUTER ALGORITHMS An algorithm is a well-defined procedure that allows a computer to solve a problem. Another way to describe an algorithm is a sequence of unambiguous instructions Every time you ask your computer to carry out the same algorithm, it will do it in exactly the same manner with the exact same result. A very simple example of an algorithm would be to find the largest number in an unsorted list of numbers. If you were given a list of five different numbers, you would have this figured out in no time, no computer needed. Now, how about five million different numbers? Clearly, you are going to need a computer to do this, and a computer needs an algorithm. Below is what the algorithm could look like. Let's say the input consists of a list of numbers, and this list is called L. The number L1 would be the first number in the list, L2 the second number, etc. And we know the list is not sorted - otherwise, the answer would be really easy. So, the input to the algorithm is a list of numbers, and the output should be the largest number in the list. The algorithm would look something like this: Step 1: Let Largest = L1 This means you start by assuming that the first number is the largest number. Step 2: For each item in the list: This means you will go through the list of numbers one by one. Step 3: If the item > Largest:
16
Embed
UNIT-1 · UNIT-1 WHAT IS COMPUTER SCIENCE? Computer Science is the study of computers and computational systems. Computer scientists deal mostly with software and software systems;
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
UNIT-1
WHAT IS COMPUTER SCIENCE?
Computer Science is the study of computers and computational systems. Computer scientists deal mostly with software and software systems; this includes their theory, design, development, and application. Principal areas of study within Computer Science include artificial intelligence, computer
systems and networks, security, database systems, human computer interaction, vision and
Although knowing how to program is essential to the study of computer science, it is only
one element of the field. Computer scientists design and analyze algorithms to solve
programs and study the performance of computer hardware and software. The problems
that computer scientists encounter range from the abstract-- determining what problems can
be solved with computers and the complexity of the algorithms that solve them – to the
tangible – designing applications that perform well on handheld devices, that are easy to
use, and that uphold security measures.
COMPUTER ALGORITHMS
An algorithm is a well-defined procedure that allows a computer to solve a problem. Another way to
describe an algorithm is a sequence of unambiguous instructions
Every time you ask your computer to carry out the same algorithm, it will do it in exactly the same
manner with the exact same result.
A very simple example of an algorithm would be to find the largest number in an unsorted list of numbers. If you were given a list of five different numbers, you would have this figured out in no time, no computer needed. Now, how about five million different numbers? Clearly, you are going to need a computer to do this, and a computer needs an algorithm.
Below is what the algorithm could look like. Let's say the input consists of a list of numbers, and this list is called L. The number L1 would be the first number in the list, L2 the second number, etc. And we know the list is not sorted - otherwise, the answer would be really easy. So, the input to the algorithm is a list of numbers, and the output should be the largest number in the list.
The algorithm would look something like this:
Step 1: Let Largest = L1
This means you start by assuming that the first number is the largest number.
Step 2: For each item in the list:
This means you will go through the list of numbers one by one.
Step 3: If the item > Largest:
If you find a new largest number, move to step four. If not, go back to step two, which means you move on to the next number in the list.
Step 4: Then Largest = the item
This replaces the old largest number with the new largest number you just found. Once this is completed, return to step two until there are no more numbers left in the list.
Step 5: Return Largest
This produces the desired result.
Notice that the algorithm is described as a series of logical steps in a language that is easily understood. For a computer to actually use these instructions, they need to be written in a language that a computer can understand, known as a programming language.
. PYTHON PROGRAMMING LANGUAGE
Python is an interpreted, high-level, general-purpose programming language. Created by Guido
van Rossum and first released in 1991, Python is dynamically typed and garbage-collected. It
supports multiple programming paradigms, including procedural, object-oriented, and functional
programming. Python is often described as a "batteries included" language due to its
comprehensive standard library.
Python 2.0, released 2000, introduced features like list comprehensions and a garbage
collection system capable of collecting reference cycles. Python 3.0, released 2008, was a major
revision of the language that is not completely backward-compatible, and much Python 2 code does
not run unmodified on Python 3.
The language's core philosophy is summarized in the document
● Beautiful is better than ugly ● Explicit is better than implicit ● Simple is better than complex ● Complex is better than complicated ● Readability counts
Syntax and semantics
Python is meant to be an easily readable language. Its formatting is visually uncluttered, and
it often uses English keywords where other languages use punctuation. Unlike many other
languages, it does not use curly brackets to delimit blocks, and semicolons after statements are
optional. It has fewer syntactic exceptions and special cases than C.
Indentation Python uses whitespace indentation, rather than curly brackets or keywords, to
delimit blocks. An increase in indentation comes after certain statements; a decrease in indentation
signifies the end of the current block.[61] Thus, the program's visual structure accurately represents
the program's semantic structure.[1] This feature is also sometimes termed the off-side rule.
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 particular programming language. Python provides a variety of operators 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 (**).
Consider the following table for a detailed explanation of arithmetic 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 result
negative. For example, if a = 20, b = 10 => a ? b = 10 / (divide) It returns the quotient after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a/b = 2
* (Multiplication)
It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the second
operand. For example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the first operand power to second operand.
// (Floor division)
It gives the floor value of the quotient produced by dividing the two operands.
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 less than 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 value of two operands is not equal, 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.
OPERATORS DESCRIPTION = It assigns the 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 assign 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 assign 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 assign 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 = 200. //= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will
assign 4//3 = 1 to a. %= It divides the value of the left operand by the value of the right
operand and assign the reminder back to 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 operator The bitwise operators perform bit by bit operation on the values of the two operands.
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 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
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 side do not point to the same object.
Operator Precedence The precedence of the operators is important 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
& Binary and.
^ | Binary xor and or
<= < > >= Comparison operators (less then, less then equal to, greater then, greater then equal to).
<> == != Equality operators.
= %= /= //= -= +=
*= **=
Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators
Python Expressions: Expressions are representations of value. They are different from statement in the fact that statements do something while expressions are representation of value. For example any string is also an expressions since it represents the value of the string as well.
Python has some advanced constructs through which you can represent values and hence these constructs are also called expressions.
Python expressions only contain identifiers, literals, and operators.
Following are a few types of python expressions:
List comprehension The syntax for list comprehension is shown below:
[ compute(var) for var in iterable ]
For example, the following code will get all the number within 10 and put them in a list.
>>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Dictionary comprehension This is the same as list comprehension but will use curly braces:
{ k, v for k in iterable }
For example, the following code will get all the numbers within 5 as the keys and will keep the corresponding squares of those numbers as the values.
>>> {x:x**2 for x in range(5)}
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
Generator expression The syntax for generator expression is shown below:
( compute(var) for var in iterable )
For example, the following code will initialize a generator object that returns the values within 10 when the object is called.
>>> (x for x in range(10))
<generator object <genexpr> at 0x7fec47aee870>
>>> list(x for x in range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Conditional Expressions You can use the following construct for one-liner conditions:
true_value if Condition else false_value
Example:
>>> x = "1" if True else "2"
>>> x
'1'
Basic Data Types in Python
Integers
In Python 3, there is effectively no limit to how long an integer value can be. Of course, it is constrained by the amount of memory your system has, as are all things, but beyond that an integer can be as long as you need it to be:
The following strings can be prepended to an integer value to indicate a base other than 10:
Prefix Interpretation Base
0b (zero + lowercase letter 'b')
0B (zero + uppercase letter 'B')
Binary 2
0o (zero + lowercase letter 'o')
0O (zero + uppercase letter 'O')
Octal 8
0x (zero + lowercase letter 'x')
0X (zero + uppercase letter 'X')
Hexadecimal 16
Floating-Point Numbers
The float type in Python designates a floating-point number. float values are specified with a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation:
Eg: >>> 4.2 4.2 >>> type(4.2) <class 'float'>
Complex Numbers
Complex numbers are specified as <real part>+<imaginary part>j. For example:
Strings are sequences of character data. The string type in Python is called str.
String literals may be delimited using either single or double quotes. All the characters between the opening delimiter and matching closing delimiter are part of the string:
>>>
>>> print("I am a string.") I am a string. >>> type("I am a string.") <class 'str'> >>> print('I am too.') I am too. >>> type('I am too.') <class 'str'>
A string in Python can contain as many characters as you wish. The only limit is your machine’s memory resources.
MIXED TYPE EXPRESSIONS
A mixed-type expressions is an expression containing operand of different type. The
CPU can only perform operation on values with the same internal representation scheme, and
thus only on operands of the same type.
Operands of mixed-type expressions therefore mustbe converted to a common type.
Value can be converted in one of two ways-by implicit(automatic) conversion,called
coercion,or by explict type conversion.
Type Conversion in Python
Python defines type conversion functions to directly convert one data type to another which is useful in day to day and competitive programming. This article is aimed at providing the information about certain conversion functions.
1. int(a,base) : This function converts any data type to integer. ‘Base’ specifies the base in which string is if data type is string. 2. float() : This function is used to convert any data type to a floating point number 3. ord() : This function is used to convert a character to integer. 4. hex() : This function is to convert integer to hexadecimal string. 5. oct() : This function is to convert integer to octal string. 6. tuple() : This function is used to convert to a tuple. 7. set() : This function returns the type after converting to set. 8. list() : This function is used to convert any data type to a list type. 9. dict() : This function is used to convert a tuple of order (key,value) into a dictionary. 10. str() : Used to convert integer into a string. 11. complex(real,imag) : : This function converts real numbers to complex(real,imag) number.