0% found this document useful (0 votes)
5 views19 pages

Python Notes

The document provides a comprehensive guide on Python installation, interpreter modes, and programming basics. It covers tokens, identifiers, keywords, literals, data types, comments, and debugging techniques in Python. Additionally, it explains variables, operators, and the use of functions like input() and print(), along with rules for naming variables and multiple assignments.

Uploaded by

sainiktha1011
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)
5 views19 pages

Python Notes

The document provides a comprehensive guide on Python installation, interpreter modes, and programming basics. It covers tokens, identifiers, keywords, literals, data types, comments, and debugging techniques in Python. Additionally, it explains variables, operators, and the use of functions like input() and print(), along with rules for naming variables and multiple assignments.

Uploaded by

sainiktha1011
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
You are on page 1/ 19

Notes python

Python Installation

Before starting programming with python, it needs to be installed. There are few
distributions of python available like CPython (set of applications such as python interpreter,
Python IDLE and Pip), Anaconda Python distribution (Comes with preloaded packages and
libraries with IDEs like Spider, Jupyter, PyCharm etc.)

Download python from www.python.org for windows suitable executable installer. The
latest version of python is 3.10After installation, you need to understand python interpreter
which is mostly used to interpret your code in Getting started with Python.

Python interpreter

Python interpreter offers two modes for coding:

1. in Interactive mode
2. in Script mode
. Working in Interactive Mode: Interactive mode allows one to type one
statement and executes the same line when the enter key is pressed. To
start working in interactive mode follow these steps: 0Step1. Click on
Start → All Programs → Python 3.10 IDLE (Python 3.10 64-bit).
shown in the below-given screen:

After typing a line or statement press enter, the interpreter will execute the line and gives the
result:

Working in Script Mode

Working in script mode is an essential part when you are Getting started with Python.

To work in script mode follow these steps:


Step 1. Click on Start → All Programs → Python 3.10 →IDLE (Python 3.10
64-bit). A python shell will appear.
Step 2. Click File New. A new window will appear with the title “untitled”.
Step 3. Type the statement(s).

Step 4. Click on Run → Run Module or press F5 key.

→ Run Module or press F5 key.


Step 5. It prompts to save the module. Save the file with .py extension.

Step 6. The python shell display output like this:

After the understanding of print() function let’s move ahead with Getting started with Python
tokens.

Q. What s are Tokens in Python?


Ans: Tokens are the least units of programs. These tokens are as following:

1. Identifiers
2. Keywords
3. Literals
4. Operators
5. Puncutators
Identifiers

Identifiers are names used in programs to identify small units of programs such as variables,
objects, classes, functions etc.
Identifiers defined by the following few rules as follows:

1.
It must start alphabets
2.
It can be a combination of numbers and letters
3.
Special characters are not allowed in identifiers name
4.
Spaces are not allowed in identifier names, underscore can be used to separate two
words
5. The meaning of Upper Case and Lower Case different should not use as identifier
names
6. Few Examples:
7. MyData, roll_no, year1 etc.
Keywords

Keywords are python reserved words used in a program. Each and every keyword conveys
special meaning to the python interpreter.
Ex.: def, False, if, elif, else, for etc.

Literals(Constants)

Literals or Constants means that an item(s) have a fixed value. There are several types of
constants or literals as follows:

1. String Literals: Ex.: ‘a’, ‘abc’, ‘my_name’, ‘t’, ‘n’ etc.


2. Numeric Literals: int, float, complex etc.
3. Boolean Literals: True or False
4. Special Literals: None
Q. How can we write comments in a python program?
Ans: A comment is text that doesn't affect the outcome of a code, it is just a piece of text to
let someone know what you have done in a program or what is being done in a block of
code. In Python, we use the hash (#) symbol to start writing a comment.
Single line comment
Using # to commenta single line statement
#Defining a variable to store a number
a =10
name=”class” #store class as a value in name variable

Multi -line comment

Multiline comment can be written using “”” or ‘ ‘ ‘as shown above

q. what are data types in python?


Ans: Data types specifies kind of value a variable can store.

Numbers
Data with numeric value falls under this category.it can be integer, float, complex.
Python automatically convert a number from on type to another.
Following are some number data type”
● Integer: integers are whole numbers (-ve, +ve or 0) with no fraction or decimal
value. Its length is dependent on the available memory. Example: 20,45
● Float: -it is a real number with floating point representation for example:
15.2,10.0
Have a look on the following code:

Using float in program


To print desired digits in decimal value post point/dot, use str.format() function. Suppose I
want to print 2 decimal place value post point/dot in the above result, I have done in the
following manner:

Result in two decimal places value after a point

● Complex: it is made up of real numbers and an imaginary number. ex: 3 + 2i


● Example:

Use of complex number in python


None: These are the special data type with NULL or no values.

Sequence: it is collection of data stored under a common name. each value is refereed
by its index value .it can be mutable or non -mutable. There are three types of data as
sequence in python.
● String: it is sequence of UNICODE characters enclosed in a single or double quotes.
multi-line strings can be created using triple quotes. for ex-“ “ ,”HELLO”,’PYTHON’,
’’’it is an interesting language’’’
Python also offers a special escape character sequence to print some non-graphic
character as following:
● LISTS: it is a sequence of heterogeneous data values arranged as elements and are
referred by an index number. These values are separated by commas and are enclosed
in square brackets[].They are mutable data types as the values of these data types can
be changed by the user after creation. Ex: List_of_name =
[“amit”,”Shweta”,”ranveer”,”sooraj”]
Marks = [59,82,45,14,85]
● TUPLES: It is a sequence of heterogeneous values arranged as elements are referred
by an index number. These values are separated by comma and are enclosed in
circular brackets().they are immutable as the values cannot be changed by the user
after creation.
● BOOLEAN : it is a data type with two built in values TRUE or FALSE.it is used in
logical evaluation. A TRUE value represents 1 and FALSE value represent 0.

Ques: What is DATA type conversion in python?


Ans: data type of one type can be converted into another type by using type built in
functions like int(), float(),str(). It is also called as TypeCasting.
There are two types of type conversions in python:
1.Explicit:This type of typecasting is done by the program to change the
result value data type. It uses the target data type as a function and a
variable is passed to convert it. Consider this example:

Implicit

It is by default conversion. As in the above program, the division operator


converts the result into float implicitly. Consider the same example:

Q. What is debugging of program in python?

Ans: A program written in python is interpreted by the python interpreter


line by line. A program may have few errors except syntax error. Sometimes
program may produce the garbage results. In this case, to understand the
logical errors user need to check the program. This checking is known as
debugging. Debugging also help to understand how the program is working.
In this post, you can read how to debug and test the program with python
IDLE.

There are two ways of writing programs in python:

1. Interactive Mode: It executes each line when you press enter.


2. Script Mode : Write program, save the program and then run the
program and get the output.
Writing a python program in script mode

1. Click on Start→All Programs→Python 3.10→IDLE (Python 3.7 32-bit).


Python IDLE screen appears.
2. Now click on File→New option.
3. Write the code for the program. Here I have written following code:

4. Save the program.

Steps to Debug
● Open Python IDLE.
● Click on Debug → Debugger option.

debug in python
● It will open a new window with the title “Debug Control” as displayed
in this screen.
● Now click set the watch window position in such a way there you can
look in the Python IDLE screen and watch window both.
● Now move on IDLE screen and Click on File→Open menu to open the
written program.
● When the program window appears, run the program. Click Run→Run
Module option.
● It will activate disabled-buttons such as Go, Step, Over, Out, Quit.
● Click on step button until it gets disabled when it gets disabled enter
desired input in the IDLE window.
● Again click on the step button to for another input. (Repeat these
steps for all input functions and enter values)
● Observe the watch window to view the value of variables with
relevant changes.
After this understanding let us see the steps for the Debugging Python
Program class 11.

Understand with this program.


python program
So follow these instructions for Debugging Python Program class 11. My first
watch window after click on Debug.

debug control
window
● As you can see python program main function is highlighted in blue
color i.e. _main_ with function header def fun1(x,y) when you run the
program.
● If you want to see the line where cursor is placed click on source
check box in watch window. Now when you click on step button,
cursor will jump to line 5 for input as displayed in the following
screen with highlighted blue color.
debug control window 1
● Click on the step, again and again, it will open run.py built-in module
for write function, then readline() function, until all buttons in the
watch window gets disabled.
● When it will get disable python IDLE screen prompt the input
message. Feed your data as prompted and do repeat the similar
steps for another input function.
● After that repeat click on a step to see the execution flow, the cursor
moves in function fun1(a,b) with values are displayed in the watch
window, look in this screen.
● After getting this screen, click on step button, the cursor moves to
first statement (highlighted in the screen shot) of fun1(a,b) function
as displayed in this screen.
debug control 2
These steps for Debugging Python Program class 11 are very important to
check the values while program debugging.

Now click on step one more time, you will get screen like this:

debug control window 3


Click on step to get this:
step in python code
Now click step button, the cursor will jump to main function and execute first
output line. Repeat few clicks on step button and observe the result.

If you want to skip some internal function execution process click on over
button. So it jumps directly to the statements written in a program.

Q. What are Operators in Python?

Ans: Operators are symbols or words used to perform the simple calculation or logical
comparison in statement or expression. Python supports these operators:

1. Unary Operators: It requires one operand Ex.: + (Positive), – (Negative), ~ (Bit


wise complement), not (Logical Negation)
2. Binary Operators: It requires more than one operators. They are as follows:
3. Arithmetic Operators: + (Addition), – (Subtraction), * (Multiplication), /
(Division), % (Modulus/Remainder), ** (Power), // (Floor division)
4. Bitwise operator: & (AND), ^ (XOR), I (OR)
5. Shift Operator: << (Shift left), >> (Shift Right)
6. Identity Operators: is, is not (compare similar identity )
7. Relational Operators: < (less than), > (greater than), <= (less than or equal to), >=
(greater than or equal to), == (equal to), != (not equal to)
8. Logical Operators: and (Logical AND), or (Logical OR)
9. Assignment Operators: =, /=, +=, -=, *=, /=, %=, **=, //=
10.Membership Operators: in, not in (use to check the variable is in sequence or not)
Q. What is program in python?
Ans: A program has important parts such as variables, statements, expressions, data types,
input and output related functions

Q. What are expressions in Python?


Ans: A small set of variables, operators, and value is known as expression. It seems like
every line of a program can be an expression.
Q. What is the use of input () Function in python?
Ans: This function is used accept value from user. Let’s look in the following:

Q. What is the use of Print () Function in python?


Ans: print() function in the above codes. It allows generating output on the screen.

The syntax of the print() function is as follows:


print(“text”,[sep=’ ‘ or end=’character’])

Basically python print() a new line when the print function is used in the program.

sometimes users want to end a line with some specific characters. In this case, the ‘end’
option is used, have a look:

Q. What are variables in python?


Ans: Variables are storage names with specific data types, used to store a value for use in the
future. It holds a value of any type such as numeric values, letters, or any other value. The
value of the variable can be changed at execution time by the user and it keeps changing as
per the need of the program.
A variable need to declare first then it will assign a value or use in input statement and
finally hold output or final result. The value of a variable manipulates any time in a
program.

Q. What are the Rules for naming a variable?

Ans: Following are the rules for naming a variable in python

Variables are similar to identifiers. Follow the identifiers naming rules while declaring
variables.
Their rules are as following:
● It must start with an alphabet
● It doesn’t contain any space or special character or symbols
● A keyword should not be used as variables
● It should be short and simple
Now let’s look at this code:

In the first code variable, a is assigned value 5. In python, when the value is assigned to the
variable, the data type of a variable is determined by python itself. So here 5 is an integer.

In the second code variable, a is declared with value 5, then value 10 is added in a and then
displayed using print() function. In the last code, two different values assigned to two
different variables a and b, 5 and 10 respectively, then added both of them along with print()
functions.
There are three main properties associated with a variable:

1. Address: Memory location address in the memory cell. To know the address of memory
location python offers id() function, that accepts a variable name as parameter.

Displaying memory address through id() function

3. Value: Assigned by the programmer or changed by the statements in a program.


3. Datatype: The type of data such as number, letters or string, etc. To print datatype
of a variable, python offers type() function.

Q. How assignment operator is used to assign a value to a variable?


Ans: The assignment operator (=) is used to assign a new value to a
variable. It takes a general form like L-value = R-value where
L-value is always a variable
R-value is a value assigned to a variable, R-value can be a value or
expression
Ex. x = 3, where L-value is x and R-value is 3.

Multiple Assignment

Python allows multiple variable assignments in one line. There are two ways
to assign multiple values:

Assign multiple values to multiple variables

The values of the variable assigned by separating them with commas. Ex.:

The values of the variable assigned by separating them with commas. Ex.:

Multiple variable assignments


Assign the same value to multiple variables

The same value assigned by using an assignment operator (=) multiple


times.

Assign a same value to multiple variable

Q. What is List in python?


Ans: List is a sequence of values of any type. Values in the list are called elements / items. List is
enclosed in square brackets. a = [1,2.3,"Hello"]
List is one of the most frequently used and very versatile data type used in Python. A number of
operations can be performed on the lists.

Q. How can we create a list?


Ans: a list is created by placing all the items (elements) inside a square bracket [ ],
separated by commas. It can have any number of items and they may be of different
types (integer, float, string)

Q. How to access elements of a list?


Ans: A list is made up of various elements which need to be individually accessed on
the basis of the application it is used for. There are two ways to access an individual
element of a list: 1) List Index 2) Negative Indexing
List Index A list index is the position at which any element is present in the list. Index
in the list starts from 0, so if a list has 5 elements the index will start from 0 and go on
till 4. In order to access an element in a list we need to use index operator [].

🡨-------length of string 6-------------🡪

‘p’ ‘y’ ‘t’ ‘h’ ‘o ‘n’ String


0 1 2 3 4 5 index

Q. What is negative indexing in python?


Ans Python allows negative indexing for its sequences. The index of -1
refers to the last item, -2 to the second last item and so on
🡨-------length of string 6-------------🡪

‘p’ ‘y ‘t’ ‘h’ ‘o’ ‘n’ String



-6 -5 -4 -3 -2 -1 Negative index

Sample program to create a list and traverse the list using index values

You might also like