Chapter1cs
Chapter1cs
me/+leBJyZDDoUw3NWRl
Introduction of Python
Python is an interpreter, interactive, object-oriented, and high-
level programming language. It was created by Guido van Rossum in
1991 at National Research Institute for Mathematics and Computer
Science Netherlands.
Features of Python
Python IDLE comprise Python Shell (Interactive mode) and Python Editor (Script mode).
● Interactive Mode: The Python commands are directly typed at >>> command prompt and
as soon as we press the enter key, the interpreter displays the results immediately, which is
known as displaying. (We cannot save file or command)
>>>10+50
60
● Script Mode: We can write multiple line of code here and get output and errors are running
code. We can open script mode using CTRL+N
Syntax of print () in python:
print(value1,value1,…………,sep=’ ‘, end=’ ‘)
Displaying Vs Printing: Getting output without using print function in interactive mode is called
displaying, And getting output using print () in interactive or script mode.
Different types of files in python:
.py, .pyw, .pyc, .pyd, .pyo, .pyz
Python character set: Character set is a set of valid characters recognized by python. A character
by letter, digit or any other special symbol.
1. Letters: A-Z, a-z 2. Digits: 0-9 3. Special Symbols:+-/*&=!@_ etc.
4. Whitespaces: Blank space, tabs (‘\t’), carriage return (Enter key), newline, form feed (skips to
the start of the next page.)
5. Other Characters: All ASCII code and Unicode character.
Tokens: A token is the smallest element of a python script that is meaningful to the interpreter.
● A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different variables)
● A variable name cannot contain spaces.
Components of Variable/Object:
Click to join our
A). Identity of the Variable / Object: It refers to the Variable’s
Telegram group:
memory location address which is unchanged once it has been
https://t.me/+leBJyZDD
created. We can check memory location using this method:
id() oUw3NWRl
*(None is a datatype with single value. It is used to signify the absence of value / Condition
evaluating to false in a situation.)
Multiple Assignments:
1) Assigning multiple values to multiple variables: x,y,z=2,3,4
2) Assigning same value to multiple variable: a=b=c=10
Keywords
Keywords are words that are already reserved for some particular purpose. The names of these
keywords should not be used as identifiers/Variable. Keywords are also called reserved words.
False, None, True, and, as, assert, 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.
1. Implicit Type Conversion: Python automatically converts one data type to another data
type. This process doesn't need any user involvement. Ex: type(5/2)
2. Explicit (forced) Type Conversion: In Explicit Type Conversion, users convert the data type
of an object to required data type. Ex: float(5), str(5),int(‘5’)
Operators and Operands
An operator is a symbol that performs an operation. +,-,*,/
An operator acts on some variables called operands.
For example, if we write a + b, the operator + is acting on two operands a and b. Python provides
some useful operators for performing various operations.
Binary Operators: Operators that operate on two operands are known as binary operators. Ex:
3+3
Unary Operators: Operators that operate on one operand are known as unary operators. Ex: -3
Types of Operators:
1. Arithmetic operators:
Example: 12 + ( 3 ** 4 – 6 ) / 2
2. Comparison operators
3. Logical operators,
4. Shorthand /Augmented Assignment operators,
5. Membership operators.
6. Identity operators,
Precedence of Python Operators
Operators Meaning
() Parentheses
** Exponent (Power)
* , /, //, % Multiplication, Division, Floor division, Modulus
+- Addition, Subtraction
==, !=, >, >=, <, <=, is, is not, in, not Comparisons, Identity, Membership operators
in
Not Logical NOT
And Logical AND
or Logical OR
What is Functions?
Advantages of Functions:
1. We can avoid rewriting the same logic/code again and again in a program.
2. We can call Python functions multiple times in a program.
3. Code becomes reusable. Click to join our
Telegram group:
How to define and call a function in python function: https://t.me/+leBJyZDD
def function_name(): oUw3NWRl
statement
print(value)
Function Calling/invoking
function_name()
What is Debugging?
Debugging is the process of finding and fixing errors or bugs in the source code of any software.
Errors are of three types – • Compile Time Error • Run Time Error • Logical Error
1. Compile time error: These errors are basically of 2 types –
1. Syntax Error: Violation of formal rules of a programming language results in syntax error.
print(“hello)
2. Semantics Error: Semantics refers to the set of rules which sets the meaning of statements.
A meaningless statement results in semantics error.
x*y=z
2. Run time Error: These errors are generated during a program execution due to resource
limitation. Such type of error are also termed as exceptions.
Ex: Division by zero, using variable which has not been defined, accessing a list element which
doesn’t exist, try to access a file which doesn’t exit.
3. Logical Error: If a program is not showing any compile time error or run time error but not
producing desired output, it may be possible that program is having a logical error.
ex: Using wrong operators like using // in place of /, giving wrong operator precedence.