What is Python
python is a general-purpose, dynamic, high-level, and interpreted
programming language. It supports Object Oriented programming approach to
develop applications. Python is a high-level, interpreted, interactive and object-
oriented scripting language. Python is designed to be highly readable. It uses
English keywords frequently where as other languages use punctuation, and it has
fewer syntactical constructions than other languages.
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. The implementation of Python was started in December 1989.
In February 1991, Guido Van Rossum published the code (labeled version
0.9.0). In 1994, Python 1.0 was released with new features like lambda, map,
filter, and reduce.
Python is copyrighted. Like Perl, Python source code is now available under
the GNU General Public License (GPL).
Features of Python
o Easy to use and Learn: Python has a simple and easy-to-understand syntax,
unlike traditional languages like C, C++, Java, etc., making it easy for
beginners to learn. o Expressive Language: It allows programmers to express
complex concepts in just a few lines of code or reduces Developer's Time.
o Interpreted Language: Python does not require compilation, allowing rapid
development and testing. It uses Interpreter instead of Compiler.
o Object-Oriented Language: It supports object-oriented programming, making
writing reusable and modular code easy.
o Open Source Language: Python is open source and free to use, distribute
and modify. o Extensible: Python can be extended with modules written in C,
C++, or other languages.
Tokens:
o The tokens can be defined as a punctuator mark, reserved words, and each word
in a statement.
o The token is the smallest unit inside the given program.
There are following tokens in Python:
o Keywords.
o Identifiers.
o Literals.
o Operators.
Python Keywords
Python keywords are unique words reserved with defined meanings and
functions that we can only apply for those functions. You'll never need to
import any keyword into your program because they're permanently
present
list of Python keywords
False Await else import pass
None Break except in raise
True Class finally is return
And Continue for lambda try
As Def from nonlocal while
Assert Del global not with
Async Elif If or yield
Python Identifiers
A Python identifier is a name used to identify a variable, function, class, module
or other object. An identifier starts with a letter A to Z or a to z or an underscore
(_) followed by zero or more letters, underscores and digits (0 to 9).
Python does not allow punctuation characters such as @, $, and % within
identifiers. Python is a case sensitive programming language. Thus, Manpower
and manpower are two different identifiers in Python.
Python Variables
Variable is a name that is used to refer to memory location. Python variable is also
known as an identifier and used to hold value.
In Python, we don't need to specify the type of variable because Python is a infer
language and smart enough to get variable type.
Variable names can be a group of both the letters and digits, but they have to
begin with a letter or an underscore.
It is recommended to use lowercase letters for the variable name. Rahul and rahul
both are two different variables.
Identifier Naming ( Note : -Rules are same for Identifier and
Variable)
Variables are the example of identifiers. An Identifier is used to identify the
literals used in the program. The rules to name an identifier are given below.
o The first character of the variable must be an alphabet or underscore ( _ ). o
All the characters except the first character may be an alphabet of lower-
case(a-z), upper-case (A-Z), underscore, or digit (0-9).
o Identifier name must not contain any white-space, or special character (!, @, #,
%, ^, &, *).
o Identifier name must not be similar to any keyword defined in the language.
o Identifier names are case sensitive; for example, my name, and MyName is
not the same. o Examples of valid identifiers: a123, _n, n_9, etc.
o Examples of invalid identifiers: 1a, n%4, n 9, etc.
Declaring Variable and Assigning Values
Python does not bind us to declare a variable before using it in the application. It
allows us to create a variable at the required time.
We don't need to declare explicitly variable in Python. When we assign any value
to the variable, that variable is declared automatically.
The equal (=) operator is used to assign value to a variable.
Python Literals
Python Literals can be defined as data that is given in a variable
or constant. Python supports the following literals:
1. String literals:
String literals can be formed by enclosing a text in the quotes. We can use both
single as well as double quotes to create a string.
Example: "Aman" , '12345'
2. Numeric literals:
Numeric Literals are immutable. Numeric literals can belong to following
four different numerical types.
Int(signed integers) :-
Numbers( can be both positive and negative) with no factional [Link]: 100
Long(long integers) :
Integers of unlimited size followed by lowercase or uppercase eg: 87032845
float(floating point) :
Real numbers with both integer and fractional part eg: -26.2
Complex(complex) :
In the form of a+bj where a forms the real part and b forms the imaginary part
of the complex number. eg: 3.14j
3. Boolean literals:
A Boolean literal can have any of the two values: True or False.
Python Operators
The operator is a symbol that performs a certain operation between two operands,
according to one definition. In a particular programming language, operators
serve as the foundation upon which logic is constructed in a programme. The
different operators that Python offers are listed here.
o Arithmetic operators
o Comparison operators
o Assignment Operators
o Logical Operators
o Arithmetic Operators
Arithmetic Operators
Arithmetic operations between two operands are carried out using arithmetic
operators. It includes the exponent (**) operator as well as the + (addition), -
(subtraction), * (multiplication), / (divide), % (reminder), and // (floor division)
operators.
Comparison operator
Comparison operators compare the values of the two operands and return a true or
false Boolean value in accordance. The following table lists the comparison
operators.
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.
<= The condition is met if the first operand is smaller than or equal to the
second operand.
>= The condition is met if the first operand is greater than or equal to the
second operand.
> 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 right expression's value is assigned to the left operand using the assignment
operators.
Logical Operators
The assessment of expressions to make decisions typically makes use of the
logical operators. The following logical operators are supported by Python.
Operator Description
and The condition will also be true if the expression is true. If the two
expressions a and b are the same, then a and b must both be true.
or The condition will be true if one of the phrases is true. If a and b are the
two expressions, then a or b must be true if and is true and b is false.
not If an expression a is true, then not (a) will be false and vice versa.
Python Data Types
Variables can hold values, and every value has a data-type. Python is a
dynamically typed language; hence we do not need to define the type of the
variable while declaring it. The interpreter implicitly binds the value with its
type.
a=5
The variable a holds integer value five and we did not define its type. Python
interpreter will automatically interpret variables a as an integer type.
Python provides various standard data types that define the storage method on
each of them. The data types defined in Python are given below.
1. Numbers
2. Sequence Type
3. Boolean
4. Set
5. Dictionary
Numbers:
1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc.
Python has no restriction on the length of an integer. Its value belongs to int
2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It
is accurate upto 15 decimal points.
3. complex - A complex number contains an ordered pair, i.e., x + iy where x and
y denote the real and imaginary parts, respectively. The complex numbers like
2.14j, 2.0 + 2.3j, etc.
Sequence Type
String
The string can be defined as the sequence of characters represented in the
quotation marks. In Python, we can use single, double, or triple quotes to define a
string.