Basic Python
Basic Python
Python provides interfaces to all major open source and commercial databases along with a better
structure and support for much larger programs than shell scripting.
Applications of Python
There exist a wide variety of applications when it comes to Python. Some of the applications are:
1. Printing Statements
We can use Python to display outputs for any code we write. To print any statement, we use print()
function in Python.
Instructions written in the source code to execute are known as statements. These are the lines of
code which we write for the computer to work upon. For example, if we wish to print the addition of
two numbers, say 5 and 10, we would simply write:
print(5+10)
This is a Python statement as the computer would go through it and do the needful (which in this
case would be to calculate 5+10 and print it on the output screen)
On the other hand, there exist some statements which do not get executed by the computer. These
lines of code are skipped by the machine. They are known as comments. Comments are the
statements which are incorporated in the code to give a better understanding of code statements to
the user. To write a comment in Python, one can use # and then write anything after it. For example:
# This is a comment and will not be read by the machine.
print(5+10) # This is a statement and the machine will print the
summation.
Here, we can see that the first line is a comment as it starts with #. In the second line, we have an
executable statement followed by a comment which is written to explain the code. In this way, we can
add comments into our code so that anyone can understand the gist of it.
In Python, there exist some words which are pre-defined and carry a specific meaning for the machine
by default. These words are known as keywords. Keywords cannot be changed at any point in time
and should not be used any other way except the default one, otherwise they create confusion and
might result in ambiguous outputs. Some of the Keywords are mentioned below:
An identifier is any word which is variable. Identifiers can be declared by the user as per their
convenience of use and can vary according to the way the user wants. These words are not defined
and can be used in any way. Keywords cannot be used as identifiers. Some examples of keywords can
be: count, interest, x, ai_learning, Test, etc. Identifiers are also case-sensitive hence an identifier
named as Test would be different from an identifier named test.
A variable is a named location used to store data in the memory. It is helpful to think of variables as a
container that holds data which can be changed later throughout programming. Just like in
Mathematics, in Python too we can use variables to store values in it. The difference here is, that in
Python, the variables not only store numerical values, but can also contain different types of data.
For example:
X = 10 # X variable contains numerical data
Letters = ‘XYZ’ # Letters variable contains alphabetic data
number = 13.95 # number variable contains a decimal value
word = ‘k’ # word variable contains a character
All of these variables contain different types of data in them. The type of data is defined by the term
datatype in Python. There can be various types of data which are used in Python programming. Hence,
the machine identifies the type of variable according to the value which is stored inside it. Various
datatypes in Python can be:
5. Python inputs
In Python, not only can we display the output to the user, but we can also collect data from the user
and can pass it on to the Python script for further processing. To collect the data from the user at the
time of execution, input() function is used. While using the input function, the datatype of the
expected input is required to be mentioned so that the machine does not interpret the received data
in an incorrect manilr as the data taken as input from the user is considered to be a string (sequence
of characters) by default.
For example:
Str = input(<String>) # Python expects the input to be of string
datatype
Number = int(input(<string>)) # Input string gets converted to an
integer value before assignment
Value = float(input(<String>)) # Input string gets converted to a
decimal value before assignment
6. Python Operators
Operators are special symbols which represent computation. They are applied on operand(s), which
can be values or variables. Same operators can behave differently on different data types. Operators
when applied on operands form an expression. Operators are categorized as Arithmetic, Relational,
Logical and Assignment. Value and variables when used with operators are known as operands.
a. Arithmetic Operators
+ Addition 10 + 20 30
- Subtraction 30 - 10 20
/ Division 30 / 10 20.0
// Integer Division 25 // 10 2
% Remainder 25 % 10 5
** Raised to power 3 ** 2 9
b. Conditional Operators
c. Logical Operators
d. Assignment Operators