0% found this document useful (0 votes)
3 views

Python Notes

The document explains the differences between Interactive Mode and Script Mode in Python, highlighting that Interactive Mode is suited for quick testing and learning, while Script Mode is better for larger projects and structured coding. It also covers various types of operators in Python, including unary, binary, arithmetic, comparison, assignment, logical, string operators, and operator precedence. Additionally, it discusses control statements, including sequential, conditional, and iterative statements, along with the importance of indentation in Python programming.

Uploaded by

adwitia.sam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Notes

The document explains the differences between Interactive Mode and Script Mode in Python, highlighting that Interactive Mode is suited for quick testing and learning, while Script Mode is better for larger projects and structured coding. It also covers various types of operators in Python, including unary, binary, arithmetic, comparison, assignment, logical, string operators, and operator precedence. Additionally, it discusses control statements, including sequential, conditional, and iterative statements, along with the importance of indentation in Python programming.

Uploaded by

adwitia.sam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PYTHON

Difference between Interactive Mode and Script Mode in Python

Interactive Mode: Script Mode:


In this mode, users can type and execute Python This mode involves writing Python code in a script
code line by line in the Python interactive shell. It file (usually saved with a .py extension) and then
allows users to quickly test snippets of code and see running the entire script at once using an interpreter.
immediate results. This is useful for larger code projects or automating
repetitive tasks.
Interactive mode is ideal for experimenting with Script mode is beneficial for writing more extensive
small pieces of code and exploring Python's programs that comprise multiple functions, classes,
features, making it a great learning tool for and modules, offering a more structured approach to
beginners. coding.
In interactive mode, code is executed line by line, Conversely, script mode requires the entire script to
allowing users to see results immediately and debug be written before running it, which can be more
errors quickly. time consuming but helps in organizational and
planning aspects of programming.
Interactive mode is well suited for quick calculations, Script mode allows for the creation of complex
simple tasks, and exploring Python libraries, software applications and projects, facilitating the
providing instant feedback for learners. development of larger scale programs.

Operators are the symbols used to perform a specific operation on different values and variables.
These values and variables are considered as the Operands, on which the operator is applied. The
valid combination of both operands and operators makes an Expression which returns a computed
result.

Python Unary operators Python operators that require one operand to perform a specific
operation are known as unary operators.

Operator Name Example Output

+ Unary Plus a = + 100; print(a) 100

- Unary Minus a = – 10 ;print (a) -10

Python Binary operators Python operators that require two operands to perform a
specific operation are known as binary operators.
Python Arithmetic operators are used to perform basic mathematical operations such
as addition, subtraction, multiplication, etc.

Example
Operator Name Description
(if a = 10 and b = 5)

+ Addition To obtain the sum of the values a + b = 15

- Subtraction To obtain the subtraction of the values a–b=5

* Multiplication To find the product of the values a * b = 50

To divide the numbers and give an output in the


/ Division a / b = 2.0
decimal form

To find the remainder when one value is


% Modulus a%b=0
divided by the other

** Exponent To calculate the power of numbers a**b =10**5 = (10)5

To divide the numbers and give an output in the


// Floor Division a//b = 2
integer form

Python Comparison/ Relational operators compare the values on either side of them
and decide the relation among them. They are also called Relational operators.

Example
Operator Name Output
(if a = 10 and b = 20)

== Equal to (a = = b) False

!= Not equal (a != b) True

> Greater than (a > b) False

< Less than (a < b) True

>= Greater than or equal to (a >= b) False

<= Less than or equal to (a <= b) True

Python Assignment operators are used to assign values to variables. Following is a


table which shows all Python assignment operators.

Operator Name Example

= Assignment Operator a = 10

+= Addition Assignment a += 5 (Same as a = a + 5)

-= Subtraction Assignment a -= 5 (Same as a = a - 5)


*= Multiplication Assignment a *= 5 (Same as a = a * 5)

/= Division Assignment a /= 5 (Same as a = a / 5)

%= Remainder Assignment a %= 5 (Same as a = a % 5)

**= Exponent Assignment a **= 2 (Same as a = a ** 2)

//= Floor Division Assignment a //= 3 (Same as a = a // 3)

Python logical operators are used to combine two or more conditions and check the
final result. There are following logical operators supported by Python language.
Assume variable a holds 10 and variable b holds 20 then

Example
Operator Description Explanation Output
(if a = 5 and b = 10)
and If both of the operands are
(a = = 5) -> True
Logical true then the condition a = = 5 and b > = 10 True
(b > = 10) -> True
AND becomes true.
or If any of the two operands
(a < 5) -> False
Logical is non-zero then the a < 5 or b ! = 10 False
(b ! = 10) -> False
OR condition becomes true.
not
Used to reverse the logical
Logical not (a ! = 5) (a ! = 5) -> False True
state of its operand
NOT

Python String Operators

Operator Description Example Result

+ It is known as concatenation operator used Str1 = “Hello” HelloWorld


to join the strings. str2 = “World”
print(str1 + str2)

* It is known as repetition operator. It Str1 = “GMGS” GMGSGMGSGMGSGMGSGMGS


concatenates the multiple copies of the same print(Str1 * 5)
string.

Python Operator Precedence

Precedence: - The precedence of an operator tells the compiler the order in which the
operators should be evaluated.

Associativity: - Term associativity tells the direction of execution of operators (“Left to


Right” or “Right to Left”) when operators in an expression have the same precedence.

Operators Meaning
() Parentheses
** Exponent
+x, -x Unary plus, Unary minus
*, /, //, % Multiplication, Division, Floor division, Modulus
+, - Addition, Subtraction
==, !=, >, >=, <, <= Comparisons operators
not Logical NOT
and Logical AND
or Logical OR

CONTROL STATEMENTS: - In computer program, statements are generally executed in


sequential manner. However, at times, the user may need to change this order of
execution by repeating or skipping the execution of a few statements, subject to a given
condition, known as Control Statements

Sequential statements refer to the statements in a program that are executed in a


sequential manner, where one statement is followed by the other, with no possibilities
of branching off to another section.
Conditional statements refer to the use of if, elif and else statements that help to control
the flow of execution by selecting specific blocks of code to execute based on conditions.

These include:
 if statement: Executes a block of code if a specified condition is true.

 if-else statement: Executes one block of code if the condition is true and another block
if the condition is false.

 elif (else if) statement: Checks multiple conditions in a sequence and executes a block
of code as soon as one of the conditions evaluates to true.

 Nested if statements: You can nest if statements within another if or else block to
create complex decision trees.
Iterative statements refer to the statements that enable the execution of a set of
statements to be repeated till the condition is True. As soon as the condition becomes
False, the control comes out of the loop and the repetition stops.

Indentation in Python
• In Python, indentation is used to declare a block. If two statements are at the same
indentation level, then they are the part of the same block.
• For the ease of programming and to achieve simplicity, python doesn't allow the use
of curly braces or parentheses for the block level code.
• Indentation is the most used part of the python programming language.
• Generally, a tab space or four spaces are given to indent the statements in python.

You might also like