You are on page 1of 8

MODULE 6

BASICS OF PYTHON

Command used for showing version


Command to find out the version of Python that you have installed in the IDLE Shell.
import sys
print (sys.version)
You can use the command python -V in the terminal/ command prompt(use uppercase V).
Keywords
Python keywords are special reserved words that have specific meanings and purposes
and can't be used for anything but those specific purposes. These keywords are always
available—you'll never have to import them into your code.You can”t use keywords as
variable names.
Eg: input,prin
Variables
Variables are containers or placeholders that can store any value
num1=100
Rules for creating variables in Python
● A variable name must start with a letter or the underscore character.
● A variable name cannot start with a number.
● A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ ).
● Variable names are case-sensitive (name, Name and NAME are three different
variables).
● The reserved words(keywords) cannot be used naming the variable.
Re-declaring a variable
We can change the value of a variable at any point inside a python program.
a=10
b=20
print(a)
c=a+b
a=30
print(a)
OUTPUT
20
30
Assigning a single value to multiple variables:
OUTPUT:

Indentation
Indentation refers to the spaces at the beginning of a code line. Where in other
programming languages the indentation in code is for readability only, the indentation in
Python is very important. Python uses indentation to indicate a block of code.

Data types
Python Data Types are used to define the type of a variable. It defines what type of data we
are going to store in a variable. The data stored in memory can be of many types.
Python has five standard data types :
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
Numbers
Numeric values are stored in number data types. Python includes three numeric types to
represent numbers: integers, float, and complex numbers.
1. int (signed integers)
2. float (floating point real values)
3. complex (complex numbers)
Example

Int
In Python, integers are zero, positive or negative whole numbers without a fractional part
and having unlimited precision, e.g. 0, 100, -10.
Float
In Python, floating point numbers are positive and negative real numbers with a fractional
part denoted by the decimal symbol, e.g. 1234.56, 3.142, -1.55, 0.23.
Complex
A complex number is a number with real and imaginary components.
For example, 5 + 6j is a complex number where 5 is the real component and 6 multiplied by
j is an imaginary component.
You must use j or J as imaginary components. Using other characters will throw syntax
errors.
Type of variables
If you are confused whether a variable is an integer or float or complex, you can use the
type(variable_name)or
isinstance(variable_name, datatype) function to find the datatype of a variable.
Ex: print(type(n1))
If c is a complex number, isinstance(c, complex)will print True, and isinstance(c, int)will
print False.
We will learn about Strings and List in detail later in this course.

Typecasting
As you can see in the table above, the Python compiler automatically decides the data type
by the value assigned to it. But in some cases, we have converted it into a specific data
type as we have done in the program of adding two numbers, as the input received will be
string by default. Converting a variable explicitly is called type casting.
Operators in python
In a program, we may have to perform different operations. They are arithmetic operations,
relational operations and logical operations.
Arithmetic Operators

Modulus operator returns the remainder of the division.


Relational(Comparison) operators

Logical operators
Logical operations are performed on boolean values. A boolean variable can have only two
values; True and False. Basic logical operations are AND, OR and NOT. Let us understand
how these operations are:

and, or, not are the keywords used in Python for performing logical operations.
Ex: 10>20 and 30<50 becomes False and True, and the final result becomes False.
Operator precedence
The following table lists all operators from highest precedence to lowest.
Operator precedence affects how an expression is evaluated.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher
precedence than +, so it first multiplies 3*2 and then add into 7.

Assignment operators:
Let us assume that the value of X is 5.

Statement
A statement in Python is a logical instruction which a Python interpreter can read and
execute. In Python, it could be an expression or an assignment statement.
Expression
An expression is a combination of operators and operands that is interpreted to produce
some other value. In any programming language, an expression is evaluated as per the
precedence of its operators. So,if there is more than one operator in an expression, their
precedence decides which operation will be performed first.

Comments in Python
Comments are human-readable descriptions in the source code of a computer program,
which are used to explain codes and make the code more readable and understandable.
Comments are meant for human understanding rather than the compiler and interpreters.
Syntax of comments varies with programming languages. In Python, anything after a # is
considered as a comment.

You might also like