0% found this document useful (0 votes)
2 views6 pages

Python Theory

Uploaded by

harvardaan43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views6 pages

Python Theory

Uploaded by

harvardaan43
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Features of Python (4 features should be known)

● Python is a high level language.


● It is a free and open source language.
● It is an interpreted language, as Python programs are executed by an interpreter.
● Python programs are easy to understand as they have a clearly defined syntax and relatively simple
structure.
● Python is case-sensitive. For example, NUMBER and number are not the same in Python.
● Python is portable and platform independent, meaning it can run on various operating systems and hardware
platforms.
● Python has a rich library of predefined functions.
● Python is also helpful in web development. Many popular web services and applications like Instagram,
Netflix, Spotify and even Google are built using Python.

Variables
A variable in a program is uniquely identified by a name (identifier). Variable in Python refers to an object — an item or
element that is stored in the memory. Value of a variable can be a string (e.g., ‘b’, ‘Global Citizen’), numeric (e.g., 345)
or any combination of alphanumeric characters (CD67). In Python we can use an assignment (the = sign is the
assignment operator) statement to create new variables and assign specific values to them.

Example : gender = 'M' message = "Keep Smiling" price = 987.9

Data Types
Every value belongs to a specific data type in Python. Data type identifies the type of data values a variable can hold
and the operations that can be performed on that data

● Integer - Int- integer numbers-–12, –3, 0, 125, 2


● Float- Float- Decimal numbers-–2.04, 4.0, 14.23
● String- Str -a group of characters. These characters may be alphabets, digits or special characters including
spaces. String values are enclosed either in single quotation (e.g., ‘Hello’) or in double quotation marks (e.g.
- “Hello”).

Operators :
An operator is a character or set of characters that can be used to perform the desired operation on the
operands and produce the final result.
The final result completely depends on the type of operators used.
For example, consider you want to perform a mathematical calculation of 5+3. Now both 5 and 3 are
called operands, + is the operator that performs addition and the final result is 8.
The different types of operators supported by Python are as follows (Important)
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
Arithmetic Operators (7 operators)
Arithmetic operators are used in performing mathematical operations such as Addition, Subtraction,
Multiplication, Division, Modulus,

% - Remainder of a division; // - Quotient of a division; ** - Exponent (to the power of)

Relational Operators (6 operators)


Relational operators are used for comparing the values. It either returns True or False according to the condition.
These operators are also known as Comparison Operators.

Operator Description Syntax

> Greater than: True if the left operand is greater than the right x>y

< Less than: True if the left operand is less than the right x<y

== Equal to: True if both operands are equal x == y

!= Not equal to – True if operands are not equal x != y


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

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

Assignment Operators
Assignment operator assigns or changes the value of the variable on its left.

Example : =

Logical Operators
There are three logical operators supported by Python. These operators (and, or, not) are to be written in lower case
only. The logical operator evaluates to either True or False based on the logical operands on either side. Every value
is logically either True or False. By default, all values are True except None, False, 0 (zero), empty collections "", (), [],
{}, and few other special values. So if we say num1 = 10, num2 = -20, then both num1 and num2 are logically True.

Difference between / and % in python


In Python, `/` and `%` are both operators used for arithmetic operations, but they serve different
purposes.

1. **Division (/):**
- The `/` operator is used for division, and it returns the quotient of the division operations.

`10 / 3` returns the floating-point result of the division.

2. **Modulus (%):**

- The `%` operator is the modulus operator, and it returns the remainder of the division
operation.

Here, `10 % 3` returns the remainder after dividing 10 by 3, which is 1.

// - Returns the integer part of the quotient

10/3 - Answer will be 3.3333333

10%3 – Answer will be 1

10//3 – Answer will be 3

In summary:

- `/` is the division operator that gives the quotient of a division operation.

- `%` is the modulus operator that gives the remainder of a division operation.

These operators are commonly used in various mathematical and programming


contexts.

● What is the difference between = and == in python


Assignment Operator (=):
● The = operator is used for assignment. It is used to assign a value to a
variable.
● Example:

Equality Operator (==):


● The == operator is used for comparison. It checks if the values on both sides
are equal.
● Example:

In this example, x == y checks whether the values of x and y are equal. Since
x is 10 and y is 5, the result is False.

● What is the difference between Syntax error and Logical


error in python
Syntax Error:

● What it is: A syntax error occurs when you don't follow the proper structure or
grammar rules of the programming language.
● Why it happens: It's like a language grammar mistake. The code doesn't make
sense to Python because you've written it in a way it doesn't understand.
● Example: Forgetting to close a parenthesis, using a wrong keyword, or
misspelling a built-in function.
● Result: The code won't run at all, and you'll see an error message before the
program starts.
Logical Error:

● What it is: A logical error occurs when the code is syntactically correct, but it
doesn't produce the expected result due to a mistake in the logic or algorithm.
● Why it happens: It's like a flaw in your plan. The code runs, but it doesn't do
what you intended it to do.
● Example: Adding instead of subtracting, using the wrong variable, or not
considering a specific condition in your code.
● Result: The code runs, but it doesn't produce the right output, and you might
not get an error message. Fixing logical errors involves rethinking and
adjusting your program.

You might also like