PYTHON NOTES
By ANUSHA
Created by Guido van Rossum
We can execute the files by adding .py at the end of the file (
just like .html at the end of an HTML file)
1. PYTHON AS A CALCULATOR
We can use Python to calculate things up by using REPL MODE [
Request Execute Print Loop ] ( this option is provided in Python Idle)
2. COMMENTS
Comments are the things that are written for our reference, and it is
ignored by Python.
They are written using # ( hashtag)
TYPES OF
COMMENTS
SINGLE LINE DOUBLE LINE
COMMENTS COMMENTS
# IS USED ‘’’ ‘’’ OR “” “” IS used
EXAMPLE-
3. PRINT FUNCTION
In Python, functions are blocks of code that do something when you
call them.
LIKE-
Function What it does Example
print() Shows output on print("Hello") → Hello
screen
len() Finds length of len("Python") → 6
string/list
type() Tells the data type type(10) → <class 'int'>
int() Converts to integer int("5") → 5
float() Converts to decimal float("5.6") → 5.6
str() Converts to string str(25) → "25"
input() Takes input from user name = input("Enter
name: ")
max() Gives biggest value max(5, 10, 7) → 10
min() Gives smallest value min(5, 10, 7) → 5
sum() Adds all numbers in list sum([1,2,3]) → 6
round() Rounds off number round(3.7) → 4
abs() Gives positive value abs(-8) → 8
sorted() Sorts items sorted([3,1,2]) →
[1,2,3]
range() Creates a sequence of range(5) → 0,1,2,3,4
numbers
Print() is used for showing the text as a output.
There are two ways to print text in Python.
Without format
• print("") is used
With format.
• print(''' ''') is used
EXAMPLE-
OUTPUT
By using the print(‘’’ “’) feature, the poem comes in a format like it
were written in the command. If print(“ “) was used, it would come
in a single line as a whole, not in the form of stanzas.
NOTE-
Python is case sensitive. ‘Print’ and ‘print’ are 2 different things. By
using ‘Print’, it will show an error as Python only recognises ‘print’.
4. VARIABLES AND DATA TYPES
A variable is the name given to store a value to identify it.
EXAMPLE-
VARIABLE-
A variable is like a box where you store data.
Example-
A=68
Ram= 90
Y= x+10
KEYWORD-
Keywords are special reserved words in Python.
You cannot use them as variable names.
Python uses them to define rules.
Examples-
If, and, or, else, def, true, false, etc..
If 56 ERROR [ cuz IF is a keyword and cannot be used as a variable to
store info/value]
IDENTIFIERS
An identifier is the name you give to something (variable, function,
class).
It’s how Python identifies it.
Rules for identifiers ( also applies to variables ):
1. Can contain letters (A–Z, a–z), digits (0–9), and underscore _.
No special characters are allowed. ( @#$%^&*() ) [ WHY?
Becoz Python identifies them for some other function.]
2. Cannot start with a digit.
3. Cannot be a keyword. [ WHY? Becoz Python identifies them for
some other function.]
4. Case-sensitive (Name ≠ name).
5. No spaces are allowed.
Examples –
Person_name= Ravi
Number= 45
78-PLAYER= MOHAN ERROR
@rider= Ishaan ERROR
In the above example, It shows that it is case sensitive. It does not
identify number-1. ( see purple text)
NOTE- All variables are identifiers, but not all identifiers are
variables.
5. DATA TYPES
Numbers
Integers (int)→ Whole numbers (10, -5)
float → Decimal numbers (3.5, -2.1)
complex → Numbers with j (2+3j)
Text
String (str)→ Words or text ("Hello", "Python")
NOTE- 1. Anything which uses “ “ is a string ex – “9” is a string
not int.
2. A string can use (‘ ‘) or (“ “) or (‘’’ ‘’’)
Sequence (Ordered collections)
list → Changeable, uses [ ] → [1, 2, "hi"]
tuple → Not changeable, uses ( ) → (1, 2, 3)
Set (Unique values, unordered)
set → Changeable, uses { } → {1, 2, 3}
frozenset → Not changeable set
Boolean
bool → True / False
Binary (for bytes data)
bytes → Cannot change
bytearray → Can change
memoryview → Special view of memory
None
Used when you want to say "no value assigned yet"
In python, it can tell us the data type by using type() feature aka (
TYPECASTING)
Example-
It shows us <class ‘str’> meaning, this data belongs to class of
Strings.
But what if I need it to be a Floating value?
I can do it like this-
Now, it shows me <class ‘float’>
This is applicable to all the data types.
6. OPERATORS IN PYTHON
Arithmetic → Maths (+, -, *, / …)
Comparison → True/False checks (= =, >, < …)
Logical → and, or, not
Assignment → store values (+ =, - = …)
EXAMPLE OF ARITHMETIC
We can use these operators for strings also like-
“Anusha” + “Sharma” = Anusha Sharma
Hare Krishna*3 = Hare KrishnaHare KrishnaHare Krishna
EXAMPLE OF COMPARISON
It gives us output ‘false’ as the comparison (y) is false or not correct.
Comparison operators are used when there is a need to compare
two given values.
Python states it’s ans in True or False.
Table for Operators-
NOTE-
“ = =” is used in comparision and “=” is used to denote the values.
ASSIGNMENT OPERATORS
Basically used to assign the values.
Examples-
Here, for the first print function, it answers 9. The code means that
we have assigned b with the value of 6, and in the next code, we
want to add 3 to the assigned value and then assign it again to b.
In the second print function, a and b values ( final assigned ones) are
calculated, and the ans -8 is given.
LOGICAL OPERATORS
These include and, or, not
The main function of these is to give an answer in True or False on
the basis of the given data.
Example-
Here, the condition x>2 and x<10 is satisfied with the value of 3.
Hence, the OUTPUT IS TRUE.
For the logical operators, there is a TRUTH TABLE FOR EACH
KEYWORD ( and, or , not)
A B A and B
TRUTH TABLE FOR AND KEYWORD.
True True ✅ True
TIP_ It means both the conditions should
True False ❌ False match.
False True ❌ False Yeh aur yeh ( same hone chayiye)
False False ❌ False
A B A or B TRUTH TABLE FOR OR KEYWORD
True True ✅ True TIP_ If one of the condition satisfies then
the ans is TRUE
True False ✅ True
Yeh ya yeh ( atleast one should be true to
False True ✅ True get output as true)
False False ❌ False
For NOT keyword, it does the opposite, like-
Not(false)= True
Not(true)= False
7. INPUT FUNCTION
By using this function we can take the user inputs.
Example-
A= input(“Enter Name”)
print(“ The name is”; A)
This would print the given data on the screen.
NOTE-
In Input, the data is taken in a string, whether it is a number or
letters.
If we add two values by input function this happens-
It adds up two strings [ “7” +”6” = 76]
To make this correct we will use, int() feature.
Hence, the problem is solved.