[Go to site: main page, start]

0% found this document useful (0 votes)
6 views13 pages

Python Programming Basics and Features

The document provides an overview of Python programming, highlighting its features, data types, and operators. It explains the characteristics of Python as an interpreted, case-sensitive language and details various tokens, including keywords, identifiers, and operators. Additionally, it covers built-in and user-defined modules, the precedence of operators, and introduces the Tower of Hanoi puzzle as a mathematical example.

Uploaded by

psitlabid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

Python Programming Basics and Features

The document provides an overview of Python programming, highlighting its features, data types, and operators. It explains the characteristics of Python as an interpreted, case-sensitive language and details various tokens, including keywords, identifiers, and operators. Additionally, it covers built-in and user-defined modules, the precedence of operators, and introduces the Tower of Hanoi puzzle as a mathematical example.

Uploaded by

psitlabid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python programming

Python is a interactive,interpreted language with simple syntax.


features of python

➤ python is a case-sensitive language. Thus, ram and ram are two different names in python.
➤ python is an interpreted language.
➤ python's interactive interpreter is also called python shell
➤ the programs written in python are easily readable and understandable.
➤ print() statement outputs an entire (complete) line and then goes to the next line for
subsequent output(s).
➤ script mode is useful for creating programs and then running them later and getting
complete output.
➤ all the programs in python are saved with the .py or . pyw extension.
Python character set:- it is a set of valid characters recognized by the python.
 Alphabets: all capital (a-z) and small (a-z) alphabets.
 Digits: all digits 0-9.
 Special symbols: python supports all kind of special symbols like, ” ‘ l ; : ! ~ @ # $ %
^`&*()_+–={}[]\.
 White spaces: white spaces like tab space, blank space, newline, and carriage return.

1 | Page
 Other: all ascii and unicode characters are supported by python that constitutes the
python character set.
Tokens
A token is the smallest individual unit in a python program. All statements and instructions
in a program are built with tokens. The various tokens in python are :
1. Keywords: keywords are words that have some special meaning or significance in a
programming language.

They can’t be used as variable names, function names, or any other random purpose.
They are used for their special features.
In python we have 33 keywords some of them are:
Try, false, true, class, break, continue, and, as, assert, while, for, in, raise, except, or,
not, if, elif, print, import, etc.
2. Identifiers: identifiers are the names given to any variable, function, class, list, methods,
etc. For their identification. Python is a case-sensitive language and it has some rules and
regulations to name an identifier. Here are some rules to name an identifier:-
 As stated above, python is case-sensitive. So case matters in naming identifiers. And
hence geeks and geeks are two different identifiers.
 Identifier starts with a capital letter (a-z) , a small letter (a-z) or an underscore( _ ). It
can’t start with any other character.
 Except for letters and underscore, digits can also be a part of identifier but can’t be the
first character of it.
 Any other special characters or whitespaces are strictly prohibited in an identifier.
 An identifier can’t be a keyword
2. Operators: these are the tokens responsible to perform an operation in an expression.
The variables on which operation is applied are called operands. Operators can be
unary or binary. Unary operators are the ones acting on a single operand like
complement operator, etc. While binary operators need two operands to operate.

2 | Page
Data types

Number:-number data type store numeric data type.

Python support three type of built-in numeric data types:-

Integer/long
Floating point numbers
Complex numbers
 int(integer):-integer represents whole numbers without any fractional part.
 Float(floating point number);-numbers with fractional part.
 Complex numbers:-they are made up of real and imaginary part.
 Bool(boolean):-it is a sub type of integer. Its posible values are TRUE or
FALSE.
 Sequence:-it is agroup of items with a well-defined ordering where each
item is indexed by an integer.

3 | Page
type():-it is used to detemine the type of variable

MUTABLE AND IMMUTABLE DATA TYPES


Variable whose values can be changed after they are created and assigned,are called MUTABLE
VARIABLES.
Example:-list dictionary,sets etc.
Variable whose values can not be changed after they are created and assigned,are called IMMUTABLE
VARIABLES.
Example:-int,float,bool,complex,string,tuple etc
Keywords: they are the reserve words used by the python.
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

MODULES
Modules is like a code library which can be used to borrow the code written by somebody
else in our python program. There are two types of modules in python:
1):Built In Modules:- these modules are ready to import and use and ships with the python
interpreter. There is no need to install such module explicitly.
There are several modules built-in modules available in Python.
Built-In modules like:
 math
 sys
 os
 random and many more.

4 | Page
2):- Python User-defined Modules
Any text file with .py extension and containing Python code is basically a module. It can
contain definitions of one or more functions, variables, constants as well as classes. Any
Python object from a module can be made available to interpreter session or another Python
script by import statement. A module can also include runnable code.
OPERATORS
Operators are special symbols or tokens which represents computational or perform various
mathematical or logical operations.
TYPES
I. Arithmetic Operator

Operator Description Syntax

Addition: adds two


+ x+y
operands

Subtraction: subtracts two


– x–y
operands

Multiplication: multiplies
* x*y
two operands

Division (float): divides the


/ x/y
first operand by the second

Division (floor): divides the


// x // y
first operand by the second

Modulus: returns the


remainder when the first
% x%y
operand is divided by the
second

5 | Page
Operator Description Syntax

Power: Returns first raised


** x ** y
to power second

Precedence of Arithmetic Operators in Python


The precedence of Arithmetic Operators in Python is as follows:
1. P – Parentheses
2. E – Exponentiation
3. M – Multiplication (Multiplication and division have the same precedence)
4. D – Division
5. A – Addition (Addition and subtraction have the same precedence)
6. S – Subtraction
II. Relational Operator

III. Operator Description Syntax

Greater than: True if the


> left operand is greater x>y
than the right

Less than: True if the


< left operand is less than x<y
the right

Equal to: True if both


== x == y
operands are equal

Not equal to – True if


!= x != y
operands are not equal

Greater than or equal to


True if the left operand
>= x >= y
is greater than or equal
to the right

6 | Page
III. Operator Description Syntax

Less than or equal to


True if the left operand
<= x <= y
is less than or equal to
the right

= is an assignment operator and == comparison operator.


Precedence of Comparison Operators in Python
In Python, the comparison operators have lower precedence than the
arithmetic operators. All the operators within comparison operators have
the same precedence order.
IV. Logical Operator

Operator Description Syntax

Logical AND: True if both


and x and y
the operands are true

Logical OR: True if either


or x or y
of the operands is true

Logical NOT: True if the


not not x
operand is false

Precedence of Logical Operators in Python


The precedence of Logical Operators in Python is as follows:
1. Logical not
2. logical and
3. logical or

V. Assignment Operator

7 | Page
VI. Operator Description Syntax

Assign the value of the


right side of the
= x=y+z
expression to the left
side operand

Add AND: Add right-side


operand with left-side
+= a+=b a=a+b
operand and then assign
to left operand

Subtract AND: Subtract


right operand from left
-= a-=b a=a-b
operand and then assign
to left operand

Multiply AND: Multiply


right operand with left
*= a*=b a=a*b
operand and then assign
to left operand

Divide AND: Divide left


operand with right
/= a/=b a=a/b
operand and then assign
to left operand

Modulus AND: Takes


modulus using left and
%= right operands and a%=b a=a%b
assign the result to left
operand

Divide(floor) AND:
Divide left operand with
//= right operand and then a//=b a=a//b
assign the value(floor)
to left operand

Exponent AND:
Calculate exponent(raise
**= power) value using a**=b a=a**b
operands and assign
value to left operand

8 | Page
VI. Operator Description Syntax

Performs Bitwise AND on


&= operands and assign a&=b a=a&b
value to left operand

Performs Bitwise OR on
|= operands and assign a|=b a=a|b
value to left operand

Performs Bitwise xOR on


^= operands and assign a^=b a=a^b
value to left operand

Performs Bitwise right


shift on operands and
>>= a>>=b a=a>>b
assign value to left
operand

Performs Bitwise left


shift on operands and
<<= a <<= b a= a << b
assign value to left
operand

Assignment Operators in Python


Let’s see an example of Assignment Operators in Python.
Example: The code starts with ‘a’ and ‘b’ both having the value 10. It
then performs a series of operations: addition, subtraction, multiplication,
and a left shift operation on ‘b’. The results of each operation are printed,
showing the impact of these operations on the value of ‘b’.

VII. Bitwise Operators


Python Bitwise operators act on bits and perform bit-by-bit operations. These
are used to operate on binary numbers.

9 | Page
Operator Description Syntax

& Bitwise AND x&y

| Bitwise OR x|y

~ Bitwise NOT ~x

^ Bitwise XOR x^y

>> Bitwise right shift x>>

<< Bitwise left shift x<<

Precedence of Bitwise Operators in Python


The precedence of Bitwise Operators in Python is as follows:
1. Bitwise NOT
2. Bitwise Shift
3. Bitwise AND
4. Bitwise XOR
5. Bitwise OR
Identity Operators in Python
In Python, is and is not are the identity operators both are used to check if
two values are located on the same part of the memory. Two variables that
are equal do not imply that they are identical.
is True if the operands are identical
is not True if the operands are not identical

Membership Operators
In Python, in and not in are the membership operators that are used to test whether a
value or variable is in a sequence.

10 | P a g e
in True if value is found in the sequence
not in True if value is not found in the sequence

ternary Operator
in Python, Ternary operators also known as conditional expressions are operators that
evaluate something based on a condition being true or false.
It simply allows testing a condition in a single line replacing the multiline if-else making
the code compact.
Syntax : [on_true] if [expression] else [on_false]

Precedenc Associativit
e Operators Description y

1 () Parentheses Left to right

2 x[index], x[index:index] Subscription, slicing Left to right

3 await x Await expression N/A

4 ** Exponentiation Right to left

Positive, negative, bitwise


5 +x, -x, ~x Right to left
NOT

Multiplication, matrix,
6 *, @, /, //, % division, floor division, Left to right
remainder

7 +, – Addition and subtraction Left to right

8 <<, >> Shifts Left to right

11 | P a g e
Precedenc Associativit
e Operators Description y

9 & Bitwise AND Left to right

10 ^ Bitwise XOR Left to right

11 | Bitwise OR Left to right

in, not in, is, is Comparisons, membership Left to


12
not, <, <=, >, >=, !=, == tests, identity tests Right

13 not x Boolean NOT Right to left

14 and Boolean AND Left to right

15 or Boolean OR Left to right

16 if-else Conditional expression Right to left

17 lambda Lambda expression N/A

Assignment expression
18 := Right to left
(walrus operator)

Precedence of Python Operators


This is used in an expression with more than one operator with different precedence to
determine which operation to perform first.
Example
10 + 20 * 30

12 | P a g e
10 + 20 * 30 is calculated as 10 + (20 * 30)
and not as (10 + 20) * 30

Tower of Hanoi
It is a mathematical puzzle game in which we use three identical rods and hand disk all of
three different sizes. The disks are arranged in the first rod such that the largest disk is placed
at the bottom and the smallest at the [Link] solve the puzzle one needs to arrange disks in the
same order in the last rod via the middle rod.
Rules
 We can move one disks at one time.
 Only the disk which is at the top can be moved and placed on the top of any other
rod.
 A disk can be placed on the top of the bigger disk only.

13 | P a g e

You might also like