Advance Computer Programming: Notes
Advance Computer Programming: Notes
ADVANCE COMPUTER
PROGRAMMING
___
Notes
Introduction to Python
Python
● Object
- the smallest and core thing that Python programs manipulate. Every object has a
type: Scalar or Non-Scalar.
● Expression
- Objects and operators can be combined to form expressions, each of which
evaluates to an object of some type, which we refer to as value.
● Shell Prompt symbol
- The symbol is >>> which indicates, interpreter is expecting the user to enter
some code of python in the shell.
● Keywords/Reserved Words in Python
- and, with, for, is, raise, assert, from, del, return, break, else, global, lambda, not,
try, except, if, elif, or, class, while, continue, exec, import, finally, pass, as, yield,
def, in, print
● Comments
- # is used to comment in Python.
● Python identifiers
- are user-defined names. They are used to specify the names of variables,
functions, class, module, etc.
● Rules to Create Identifiers
- can’t use reserved keywords as an identifier name
- contain letters in a small case (a-z), upper case (A-Z), digits (0-9), and
underscore (_)
- can’t begin with a digit
- can’t contain only digits
- can start with an underscore
- no limit on the length of the identifier name
- case sensitive
● Valid Identifiers
- ab10c: contains only letters and numbers
- abc_DE: contains all the valid characters
- surprisingly but Yes, underscore is a valid identifier
- abc: identifier can start with an underscore
● Invalid Identifiers
- 99: identifier can’t be only digits
- 9abc: identifier can’t start with number
- x+y: the only special character allowed is an underscore
- for: it’s a reserved keyword
● Multiple Assignments
- Python allows you to assign a single value to several variables simultaneously.
For example −a = b = c = 1
● Python Numbers
- int (signed integers)
- long (long integers, they can also be represented in octal and hexadecimal)
- float (floating point real values)
- complex (complex numbers)
● Python Strings
- identified as a contiguous set of characters represented in the quotation marks.
- allows for either pairs of single or double quotes.
● Python List
- A list contains items separated by commas and enclosed within square brackets
([]).
- lists are similar to arrays in C. One difference between them is that all the items
belonging to a list can be of different data types.
● Python Tuples
- A tuple consists of a number of values separated by commas.
- Unlike lists, however, tuples are enclosed within parentheses.
● Python Dictionary
- kind of hash table type.
- enclosed by curly braces ({ }) and values can be assigned and accessed using
square braces ([]).
● Operators
- the constructs which can manipulate the value of operands.
- Relational operators, compare the values on either sides of them and decide the
relation among them
- Bitwise operator, works on bits and performs bit by bit operation.
1. Sequential statements
- are a set of statements whose execution process happens in a sequence.
- The problem with sequential statements is that if the logic has broken in any one
of the lines, then the complete source code execution will break.
2. Decision control statements or branching statements
- allows a program to test several conditions and execute instructions based on
which condition is true.
- If statements are control flow statements that help us to run a particular code,
but only when a certain condition is met or satisfied. A simple if only has one
condition to check.
- if-else statement evaluates the condition and will execute the body of if the test
condition is True, but if the condition is False, then the body of else is executed.
- Nested if statements are an if statement inside another if statement.
- if-elif-else statement is used to conditionally execute a statement or a block of
statements.
3. Repetition statements
- used to repeat a group(block) of programming instructions.
- for loop is used to iterate over a sequence that is either a list, tuple, dictionary,
on a set.
- while loops are used to execute a block of statements repeatedly until the given
condition is satisfied. Then, the expression is checked again and, if it is still true,
the body is executed again. This continues until the expression becomes false.
Python Functions
- A way of isolating code that is needed in more than one place by refactoring it to make it
much more modular.
- Defined with the def statement or def keyword.
Arguments
Recursion