Python Fundamental
Python Fundamental
History of Python
Python was developed by Guido van Rossum in the late eighties and early nineties at the National
Research Institute for Mathematics and Computer Science in the Netherlands.
Python Features
Python's features include −
Easy-to-learn − Python has few keywords, simple structure, and a clearly defined syntax. This
allows the student to pick up the language quickly.
Easy-to-read − Python code is more clearly defined and visible to the eyes.
Easy-to-maintain − Python's source code is fairly easy-to-maintain.
A broad standard library − Python's bulk of the library is very portable and cross-platform
compatible on UNIX, Windows, and Macintosh.
Interactive Mode − Python has support for an interactive mode which allows interactive testing and
debugging of snippets of code.
Portable − Python can run on a wide variety of hardware platforms and has the same interface on
all platforms.
Extendable − You can add low-level modules to the Python interpreter. These modules enable
programmers to add to or customize their tools to be more efficient.
GUI Programming − Python supports GUI applications that can be created and ported to many
system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X
Window system of Unix.
Tokens(Lexical Units): The smallest individual unit in a program is known as a token. Python has following
types of token:”-
1. Keywords
2. Identifiers
3. Literals
4. Punctuators
5. Operators
1. Keywords: Keywords are the words that convey a special meaning to the language compiler. These are
reserved for special purpose and must not be used as normal identifier names. For ex: byte, if, True, False
etc.
2. Identifiers: These are fundamental building blocks of a program and are used as the general terminology for
the names given to different parts of the program like variables, objects, classes, functions, arrays etc.
Identifier forming some rules related to C++:
a. It can have alphabets, digits and underscore and dollar sign character.
b. They must not be a keyword or Boolean literal or null literal.
c. They must not begin with a digit.
2
Operators: The operations are represented by operators and the objects of the operations are referred
to as operands.
1. Unary Operators: Operators that act on one operand are referred to as Unary Operators.
- Unary minus
~ Bitwise complement
++ Increment operator
-- Decrement operator
! Logical negation
2. Binary Operators:
Data types
are the classification or categorization of data items. It represents the kind of value that tells what
operations can be performed on a particular data. Following are the standard or built-in data type of
Python:
4
Numeric
In Python, numeric data type represent the data which has numeric value. Numeric value can be
integer, floating number or even complex numbers. These values are defined
as int, float and complex class in Python.
Integers – This value is represented by int class. It contains positive or negative whole numbers
(without fraction or decimal). In Python there is no limit to how long an integer value can be.
Float – This value is represented by float class. It is a real number with floating point representation.
It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative
integer may be appended to specify scientific notation.
Complex Numbers – Complex number is represented by complex class. It is specified as (real part)
+ (imaginary part)j. For example – 2+3j
Sequence Type
In Python, sequence is the ordered collection of similar or different data types. Sequences allows to
store multiple values in an organized and efficient fashion. There are several sequence types in Python
String
List
Tuple
1) String
In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of one or
more characters put in a single quote, double-quote or triple quote. In python there is no character data
type, a character is a string of length one.
Creating String
Strings in Python can be created using single quotes or double quotes or even triple quotes.
Boolean
Data type with one of the two built-in values, True or False. Boolean objects that are equal to True
are truthy (true), and those equal to False are falsy (false). But non-Boolean objects can be evaluated in
Boolean context as well and determined to be true or false. It is denoted by the class bool.
3. Comments: These are the additional readable information to clarify the source code. This is not a part
of execution of the code.
a. Single line comment: This starts with # symbol.
Ex: # this is my first program of python.
# python is simpler than C++
b. Multiline comment: Comments enclosed in triple qotes (“ “ “) or tiple apostropje(‘ ‘ ‘) are called
docstrings .
Ex: ‘’’ this is my first program of python.
python is simpler than C++ ‘’’
4. Functions: A function is a code that has a name and it can b resued by specifying its name in the program
where needed.
Ex: SeeYou()
5. Blocks and indentation: Some times a group of statements is part of another statement or function. Such
a group of one or more statements is called block or code-block or suit.
Ex:
if(b<5):
print(“Value of b is less than 5”)
print(“Thank you”)
6
Variable: A variable represents named location that refers to a value and whose values can be used and
processed during program run.
70
Ex: marks=70 marks
Creating a Variable: Python creates the variable of the type similar to the type of value assigned.
PYTHON VARIABLES ARE NOT CREATED LIKE CONTAINER LIKE ANOHTER LANGUAGE LIKE C, C++
Lvalues and
Rvalues: lvalue: expression that can come on the lhs(left hand side) of an assignment.
rvalue: expression that can come on the rhs(right hand side) of an assignment.
We can say
a=10
But We cannot say
10=a
Multiple assignment:
Input function: The input() function always returns a value of String type.
Ex: a=5
print(“Double of “,a , “is” ,a*2)
Output: Double of 5 is 10
In python we can break any statement by putting a \ is the end and pressing Enter key, then completing
the statement in next line.
Ex: print(“Hello “, \
How are you”)
In Python, operator precedence rules specify the order in which operators are evaluated in an expression. Python has a
well-defined set of operator precedence rules that determine the order in which arithmetic and logical operators are
evaluated. Operators with higher precedence are evaluated first, and those with lower precedence are evaluated later.
The following table lists Python’s operators in order of precedence, from highest to lowest:
Operator Description
() Parenthisis
** Exponentiation
^ Bitwise XOR
I Bitwise OR
or Logical OR
>>>x = 5 + 3 * 2 ** 2 // 4
Output:
8
>>>7*(8/(5//2))
Output:
28.0
Type Conversion
In this article, we are discussing type conversion in Python. It converts the Py-type data into another
form of data. It is a conversion technique. Implicit type translation and explicit type converter are
Python's two basic categories of type conversion procedures.
8
i) Syntax errors-Python has itsown rules that determine its syntax. The interpreter
interprets the statements only if it is syntactically correct. If any syntax error is present, the
interpreter shows error message(s) and stops the execution there. For example,
parentheses must be in pairs, so the expression (10 + 12) is syntactically correct, whereas
(7 + 11 is not due to absence of right parenthesis. Such errors need to be removed before
the execution of the program.
ii) Logical errors-A logical error is a bug in the program that causes it to behave
incorrectly. A logical error produces an undesired output but without abrupt termination of
the execution of the program.
For example, if we wish to find the average of two numbers 10 and 12 and we write the
code as 10 + 12/2, it would run successfully and produce the result 16. Surely, 16 is not the
average of 10 and 12. The correct code to find the average should have been (10 + 12)/2 to
give the correct output as 11.
iii) Runtime errors-A runtime error causes abnormal termination of program while it is
executing. Runtime error is when the statement is correct syntactically, but the interpreter
cannot execute it.
For example, we have a statement having division operation in the program. By mistake, if
the denominator entered is zero then it will give a runtime error like “division by zero”.