You are on page 1of 6

FUNDAMENTALS OF PYTHON LANGUAGE

Python is a high-level, interpreted object-oriented programming language developed by Guido


Van Rossum in early Nineties at National Research Institute for Mathematics in Netherlands.

It's main features are:-

 Simple & easy to learn


 It involves less code
 It is utilized by every major technology company. Python is based on ABC Language &
BASIC lanugage.
 Platform Independent which means it can run across different platforms like Windows,
Linux/Unix, Mac Os and other operating systems.

Installing Python:-

For installing Python, we must first download the installation package of requried version from
the link/URL.
Https://www.python.org/downloads/

Python is used by Google Search engine, Youtube etc. It is widely used for system programming,
GUI Programming, Internet Scripting/Web, gaming, text processing, network programming,
commercial robots and in space & scientific applications.

Interacting with Python(Python IDLE)

To write & run python programs interactively we can either use command line window or IDLE
(Integrated Development Learning Environment). IDLE is a program that allows the user to edit,
run, browse and debug a python program from a single interface.

Python Shell:- When we start Python IDLE, it always starts up in the shell- Python shell is an
interactive window where you can type the python code to see the output in same window. The
interactive interpreter of python is termed as python shell
Python IDLE comprises Python Shell (Interactive mode) and python Editor(script mode). The
three greater than signs(>>>) are called the prompt or python command prompt.

Python Editor Window (Working in Script Mode):- Python Script mode or Python Editor is a
standard text editor where you can type or edit the code. The files are saved by .py extension
automatically.

Exiting Python:- In order to exit the Python Command prompt, click Ctrl+Z and press Enter, or
type quit() or exit() & press enter key.

Python Programming Fundamentals:- A python program, sometimes called a script, is a


sequence of definitions & commands which are executed by python interpreter known as python
shell.
Syntax:- A set of rules that defines how to write & interpret a python program.

Data Types:- It is the type of value stored by an identifier or a variable.

Variable:- A name given to a memory location where a user can store or access any value. In a
variable, a value can be changed during the execution of a program. Every variable has a type
and this type defines the format and behaviour of the variable. e.g x=5
means x is a variable and it contains a value 5. Actually here x is the name of memory location
which holds value 5.

Note:- In python, every element is termed as an object. Hence, a variable is also an object in
python.

A variable/object has three main components:-

 Identity of variable/object.
 Type of variable/object.
 Value of variable/object.

Identity of Variable:- It refers to the objects address in memory which does not change once
it is created. The address of an object can be checked using function 'id'.

Syntax: id(object)
e.g id(x) will display something like
12406923-> Address of memory location of x

Type of variable/object:- It refers to the value it contains and the type of allowable operations on
these values. It can be checked by 'Type' function.
Syntax:- Type(variable or object)

Value of variable/object:- We assign value to a variable through assignment (=) operator.

e.g x=5

There should be one & only one variable on the left-hand side of the assignment operator. This
variable is called Left value or L-value. There can be any valid expression on the right-hand side
of assignment operator. It is called Right-value or R-value.

There are different types of data types in Python.

Number:- It stores numerical values. Python supports three in-build numeric data types.

 Integer (int) :- It represents whole number without any fractional part. They can be -ve or +ve
number which have unlimited size in Python. e.g 2,-67,239,-67493 etc.

Note:- Range in python can be from -2147483648 to 2147483647 and long integer has unlimited
range subject to available memory.
 Floating point (float):- It denotes numbers with fractional part i.e which contains a decimal
point. e.g 3.5, -4.57, 6.0, 1893.56 etc.
 Complex numbers:- These are made up of pairs of real and imaginary numbers. They are of
the form x+yj or x-yj.
Note:- Complex numbers are not extensively used in python programming.

Boolean (bool):- It represents one of the two possible values, True or False. Any expression
which can be True or False has the data type bool. e.g 5<10 will give True.

Variable Names:- There are certain rules which are to be followed for a valid variable name.

i. A variable name must start with an alphabet or an underscore(_).

ii. It can contain any number of letters, digits & (_).

iii. Keyword cannot be used as variable name.

iv. It should be short & descriptive.


Keywords:- Keywords are reserve words which have specific meaning for interpreter. For
checking the list of keywords, following statements can be given :-
import keyword
print(keyword,kwlist)
Expression:- An expression can be a value, a variable, a combination of variable & value etc.
e.g a , 2 , a+b , (a+b)-2 etc.
When converting some algebraic expressions to programming expressions, you many have to
insert parenthesis that do not appear in algebraic expressions.
e.g z = 3bc + 4 will be written as z = 3 * b * c + 4
Operators:- Operators are special symbols which can be applied on operands which can be values
or variables.
Operators can be categorized into:-

i. Arithmetic operators

ii. Relational operators

iii. Logical operators


Arithmetic operators:- Operators which are used to solve arithmetic expressions. These are
further divided into two types.

i. Unary
ii. Binary
Unary operators:- Operator which requires only one operand to be operated upon. e.g ( - , +, !)
Like -2, +3 , ! True etc.
Binary Operators:- Operator which requires two operands to be operated upon e.g (+ , - , * , / , //
, % , **)
Usage of some operators :
/ - Division e.g 5/2 = 2.5
// - Integer Division e.g 5//2 = 2
% - Remainder or modulus e.g 5%2 = 1
Relational operators:- Operators used for comparing two expressions and results in either True or
False.
e.g < , > , <= , >= , == , !=
Logical operators:- Logical operators are used to connect two relational expressions, resulting in
True or False. e.g and , or , not(unary).
‘and’ operator results in true if both the operands are true.
‘or’ operator results in true if any of the operands is true.
‘not’ operator results in true if the operand is false & vice-versa.
Precedence of operators:- Precedence means priority of execution of operators in an expression
-,+,!
* , / , // , % , **
and
or

Shorthand assignment operators:- Shorthand or Compound assignment operator is a combination


of a binary operator & assignment operator. E.g. +=, -=, /=, *=, //= etc.
X + = 2 can be expanded as x = x + 2
User input:- While writing a program, if we need to fetch some data from the user, then we must
use Input().
input() :- input() is used to get data from the user. It enables us to accept an input string from the
user without evaluating its value.
e.g name=input('Enter your name:')
print('Welcome, name)
The above lines will print the value inputted in 'name' variable during the execution of program.
Note:- Result of input() is a string and if we want the result to use in mathematical operations,
then we must use int() to convert text value to integer.
Usage of int():-
e.g. num1=int(input("Enter first number:")
num2=int(input("Enter second number:")
result=num1+num2
print("Result of addition is ", result)
eval() :- This function is used to evaluate the value of a string. It takes string as an argument,
evaluates this string as a number and returns numeric result.
e.g num1=input('Enter first number:')
num1=eval(num1)
User defined function:- function is a name given to a set of statements which perform a specific
task. Syntax: def functionname(list of parameters):
statements
Indentation in python:- Since python functions don't have any explicit beginning or end, like to
indicate the start and stop for the function, they have to rely on this indentation.
Tokens:- Token is the smallest element of a python script that is meaningful to the interpreter.
Different tokens are:-

i. Identifiers

ii. Keywords

iii. Literals

iv. Operators

v. Delimiters
Identifiers:- The name of any variable, constant, function or module is called an identifier.
e.g x , xyz , a12 etc.
Keywords:- The reserved words of python which have a special meaning for the interpreter.
e.g print , if , for etc.

Literal: A fixed numeric or non-numeric value is called a literal. e.g 2, 'abc' , 2.43 etc.
Delimiters:- Symbols which can be used as separator of values or to enclose some values.
e.g () , {} , ',’ , : etc.
Comments:- Comments are statements in a script that are ignored by the python interpreter and,
therefore have no effect on the actual output of code. It makes the code more readable &
understandable.
Comment starts with '#' anywhere in a line.

You might also like