What is Python?
-----
Python is a popular programming language. It was created by Guido van Rossum, and
released in 1991.
It is used for:
web development (server-side),
software development,
mathematics,
system scripting.
What can Python do?
Python can be used on a server to create web applications.
Python can be used alongside software to create workflows.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python can be used for rapid prototyping, or for production-ready software
development.
Why Python?
Python works on different platforms (Windows, Mac, Linux, Raspberry
Pi, etc).
Python has a simple syntax similar to the English language.
Python has syntax that allows developers to write programs with fewer
lines than some other programming languages.
Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
Python can be treated in a procedural way, an object-oriented way or a
functional way.
PYTHON BASICS---
Comments
Comments are the statements which are incorporated in the code to
give a better understanding of code statements to the user.
There are two types of comments in python.
1. Single Line 2. Multi Line
Single Line comment
A single-line comment is used to add some explanatory text in the
program for better understanding of the next line. A # sign is used to
write a single line comment in the python program For example,
# statement to add two numbers
res = 6 + 7
#Print the result
print(res)
Multiline comments
The multiline comments are written in python using triple quotes. You
can write number lines starting with triple quotes and end with triple
quotes.
For example,
'''Write a python program to display the difference between two
numbers, the first number should be larger than second number'''
n1=5 n2=2 res=n1 - n2
print(res)
Keywords (Reserved Words)
Keywords are the reserved words or pre-defined words with a special
meaning to the machine by default. So the user cannot use them
anywhere else or it cannot be changed or modified in the entire
program. They are always case-sensitive.
Identifiers
Identifiers are names used in programs to identify small units of
programs such as variables, objects, classes, functions etc. Identifiers
defined by the following few rules as follows:
1. It can be a combination of numbers and letters
2. It must start alphabets or underscore
3. Special characters are not allowed in identifiers name except
underscore
4. Spaces are not allowed in identifier names, underscore can be used to
separate two words
5. Upper Case and Lower Case letters are treated differently
Examples:
MyData, roll_no, year1 etc…
Variables---
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.
For example, a=10 b=20
These declarations make sure that the program reserves memory for two variables
with the names a and b. The variable names stand for the memory location.
Datatypes—
In program, you have a choice to use any type of data such as real numbers,
numbers with decimals, numbers without decimals, text, etc. These type of data is
defined by datatype in Python.
Python Input and Output
We use the print() function to output data to the standard output device (screen). We
can also output data to a file. An example is given below
a = "Hello World!" print(a)
The input() function is used to accept values from the user at runtime. This function
accepts and returns the text data by default. Therefore, you need to specify the data
type if you want to use numbers or any other datatype. This process is known as
typecasting
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.
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 operators. Values and variables when
used with operators are known as operands
Conditional Statements—
Conditional statements help the machine in taking a decision according to the condition
which gets fulfilled. There exist different types of conditional statements in Python.
Simple if Statement
If you have only one condition to be execute or one possibility of output, simple if statement
is useful for the same. Simple it executes the True condition block. Suppose, If gate is open
you are allowed to go inside!
Let’s see the syntax
if <condition>:
statement(s)
Example:
gate_status = "open"
if gate_status=="open":
print("Entry is permitted")
if else statement
if <condition>:
statement(s)
else:
statement(s)
gate_status = "open"
if gate_status=="open":
print("Entry is permitted")
else:
print("You have to go back to your home, Bye - Bye")
if- elif- else ladder
Python if-elif-else ladder is used in the case when you have more than two choices or
possibilities. The elif is a short form of else if. The ladder means it will continue upto n
possiblities.
if <condition1>:
statement(s)
elif <condition2>:
statement(s)
elif <condition3>:
statement(s)
....
....
....
else:
statement(s)