Python - IITM ๐
Week 1 Lecture Notes ๐๏ธ
PRINT ๐จ๐ฝโ๐ป
- Numbers are treated as numbers only when put without apostrophe/speech marks
- Strings require either single or double quotes
- Uses only round brackets
()
- Uses comma to separate strings/variables
VARIABLES ๐ค
๐ก Use well defined names for variables instead of a/b/c etc.
Input commands
del
- Used to delete a variable
DATA TYPES 1 ๐งฉ
- ๐ข int - Integer
- ๐ฃ float - Decimal number
- ๐ค str - String- basically any letter or group of letters
- ๐ list -
- Self explanatory
- uses square bracket [ ]
- uses comma to separate list items
- indexing starts from 0 and not 1
- negative indexing starts from -1 for the last elemen
DATA TYPES 2
bool
- Boolean - Takes either true or false as input. T of true and F of false must always be capital
Converting data types:
OPERATORS AND EXPRESSIONS
๐ก Follows BODMAS
MATHEMATICAL
+
- Sum/Union for lists-
- Subtract/
- Returns Float Quotient
*
- Multiplication//
- Floor Division - Returns Integer Quotient
RELATIONAL
<
, >
, >=
, <=
- Self Explanatory==
- Compares the two operands and returns the value as either True or False
LOGICAL
and
, or
, not
- Logic gates. Returns either True or False as value
STRINGS
๐ก Strings are treated like a list. Their letters are indexed just like lists. Starting from 0.
๐ก we can use only + and * operators on strings and not - and /
WEEK 2 Lecture Notes ๐๏ธ
- Comment - Starts with # . Isn’t read by computer.
Short hand operator
-=
, *=
, /=
- Self explanatory
Special operator
in
- In operator - used to find some specific value in a string or variable or list, etc.
Chaining operator
When two or more relational operators are used together.
Escape characters
\t
- Used to add tab space. Can be used repetitively. Ex - \t\t\t , etc
\n
- New line in the same print command
Quotes
โ''
โ''
- Multi line string
โ''
โ''
- Multi line comment
String Methods
Methods | Description | Code x = ‘pytHo sTring mEthOds | Output |
---|
lower() | converts a string into lower case | print(x.lower()) | python string methods |
upper() | converts a string into upper case | print(x.upper()) | python string methods |
capitalize() | converts the first character to upper case | print(x.capitalize()) | python string methods |
title() | converts the first letter of each word to upper case | print(x.title()) | python string methods |
swapcase() | swaps upper to lower and lower to upper case in the string | print(x.swapcase()) | python string methods |
Method | Description |
---|
islower() | Returns True if all characters in the string are lower case |
isupper() | Returns True if all characters in the string are upper case |
istitle() | Returns True if the string follows the rule of a title |
Code | Output |
---|
x='python' print(x.islower()) | True |
x='Python' print(x.islower()) | False |
x='PYTHON' print(x.isupper()) | True |
x='PYTHoN' print(x.isupper()) | False |
x='Python String Methods' print(x.istitle()) | True |
x='Python string methods' print(x.istitle()) | False |
Methods | Description |
---|
isdigit() | Returns True if all characters in the string are digit |
Code | Output |
---|
x='123' print(x.isdigit()) | True |
x='123abc' print(x.isdigit()) | False |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
Idioms & Phrases | Meaning |
---|
Rank and File | Ordinary People |
An interesting cipher: More on strings ๐ค
- ๐
alpha.index(n[i])
- finds index of the โiโth letter of โ n โ in โ al โ - ๐
alpha.index(n[i])+1
- increases the index of that specific letter. - ๐ we used
%26
to make sure the code doesnโt break if our โ n โ has z in it. - ๐ withouth
%26
itโll show index error as thereโs no letter ahead of z in โ al โ - ๐
%26
= we are calling out reminder after we divide our index number with 26 - so if โzโ i.e. 26 is divided by 26 itโll give - ๐ 0 as reminder. This 0 = index 0 = โaโ
- ๐
(al[(al.index(n[i])+1)%26])
- gives us the +1 shifted letter of n - ๐ we store it in an empty string
t
and append it with the letters shifted +1
of our n
Replit Link
If statement
if, else and elif(else-if)
graph TD;
Start-->A;
A-->Time;
Time-->Price1;
Time-->Price2;
Price1-->Coach;
Price1-->Train;
Price2-->Red_Eye_Flight;
Price2-->DayTime_Flight;
Coach-->Arrive_City_B;
Train-->Arrive_City_B;
Red_Eye_Flight-->Arrive_City_B;
DayTime_Flight-->Arrive_City_B;
Arrive_City_B-->End;
Week 3 Lecture Notes ๐๏ธ
While loop โฟ
- It gets executed till the condition is met.
- It quits when the condition is met.
Compute Factorial ๐จโ๐ป
- When the first time while loop runs f=1*1 and i is incremented by 1. Hence i becomes 2.
- Now the second time runs i is 2. Hence f=1*2=2. Again i is incremented and becomes 2+1 = 3.
- This continues till i>n
Practice questions on While loop ๐ง
Problem 1: Find the factorial of the given numberโ
- Find factorial of a given number:
Test Cases
no. | Input | Expected Output |
---|
1 | 5 | 120 |
2 | 2 | 2 |
3 | 0 | 1 |
4 | -7 | Not defined |
Problem 2: Find the number of digits in a given numberโ
- Find the number of digits in a given number.
Test Cases
no. | Input | Expected Output |
---|
1 | 5 | 120 |
2 | 2 | 2 |
3 | 0 | 1 |
4 | 0 | 1 |
5 | 0 | 1 |
Problem 3: Reverse the given numberโ
Test Cases
no. | Input | Expected Output |
---|
1 | 5 | 120 |
2 | 2 | 2 |
3 | 0 | 1 |
4 | 0 | 1 |
5 | 0 | 1 |
Firstly, the num is divided by 10 to get its remiander - %
is used for that. When we divide any number by 10 to get its remainder the remainder is always the last digit of the number. So r is the last digit of the input number now.
sum=sum*10+r
puts the last digit of the input number r as the first digit of the reverse number.
num=num//10
gives the int quotient of the initial number when divided by 10. Any number when divided by 10 gives the integer quotiet the nummber itself bar the last digit of it. i.e. 1234//10 gives 123.
The sum=sum*10+r
for 1234 looks like sum=4 for the first cycle. sum=4*10+3
for the second cycle. sum=43*10+2
for the third cycle. sum=432*10+1
for the last cycle.
Find if the number is palindrome or not?