1. Flowcharts and Pseudocode:
Flowcharts: These remain the same as visual representations of the solution.
Pseudocode: Still used for writing generic logic, as in C++, but now in Python syntax.
2. What is Python?
- Python is a high-level programming language, developed by Guido van Rossum.
3. Variables in Python:
- Variables in Python are also containers to store values, but in Python, you don't need to explicitly declare the type.
4. Data Types in Python:
int (for integers)
str (for characters or strings)
float (for floating point numbers)
bool (for boolean values: True or False)
5. Basic Syntax in Python:
# Python doesn't need #include statements
print("Hello World")
6. Operators in Python:
Arithmetic Operators:
+
,-
,*
,/
,//
(floor division),%
,**
(exponentiation)Relational Operators:
==
,!=
,<
,>
,<=
,>=
Logical Operators:
and
,or
,not
Unary Operators: Python uses
++
or--
(pre/post-increment/decrement) directly, as Python does not support them. You can mimic increment/decrement via+=
or-=
.
Example:
a += 1 # equivalent to a++
a -= 1 # equivalent to a--
7. Conditional Statements:
- if and else:
if condition:
# do something
else:
# do something else
- elif (else if in Python):
if condition1:
# block 1
elif condition2:
# block 2
else:
# block 3
- for loop:
for i in range(5): # from 0 to 4
print(i)
- while loop:
i = 0
while i < 5:
print(i)
i += 1
- do-while loop (not available natively in Python, but can be mimicked):
while True:
# Code here
if condition:
break
8. Ternary Statements:
result = "if" if condition else "else"
9. Functions in Python:
Python functions are declared using the def
keyword. Python doesn't specify return types like C++.
Example:
def hello():
print("Hello World")
hello()
10. Memory - Stack vs. Heap:
In Python, memory management is automatic, handled by Python’s garbage collector. Variables are usually managed on the heap, and references to them are stored on the stack.
11. Pass by Value and Pass by Reference:
- Python always passes arguments by reference. However, for immutable types (like integers, strings), it behaves like pass by value.
Example:
def increment(num):
num += 1
return num
x = 5
x = increment(x)
print(x) # Output: 6
12. Python Standard Library (STL):
Python has a built-in Standard Library that provides many useful modules (like math
, os
, sys
, etc.).
13. Arguments and Parameters in Functions:
Arguments are values passed to the function.
Parameters are the variables defined when the function is declared.
Example:
def sum(a, b):
return a + b
result = sum(5, 7)
14. Binomial Coefficient (nCr):
In Python, you can calculate nCr
using the math library for factorials.
import math
def nCr(n, r):
return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))
print(nCr(5, 2)) # Output: 10
15. Binary Number System:
Converting Binary to Decimal:
def binary_to_decimal(num):
ans = 0
pow = 1
while num > 0:
rem = num % 10
num = num // 10
ans += rem * pow
pow *= 2
return ans
# Input and Output
num = int(input())
print(binary_to_decimal(num))
Converting Decimal to Binary:
def decimal_to_binary(num):
ans = 0
pow = 1
while num > 0:
rem = num % 2
num = num // 2
ans += rem * pow
pow *= 10
return ans
# Input and Output
num = int(input())
print(decimal_to_binary(num))
16. Common Binary Numbers:
Same as in C++, the binary equivalents are:
0 ->
0
1 ->
1
2 ->
10
3 ->
11
4 ->
100
5 ->
101
6 ->
110
7 ->
111
8 ->
1000
9 ->
1001
17. Bitwise Operators:
In Python, the bitwise operators are the same:
AND:
&
OR:
|
XOR:
^
Left Shift:
<<
Right Shift:
>>
Examples:
# Bitwise AND
a = 5 # 0101
b = 3 # 0011
print(a & b) # Output: 1
# Bitwise OR
print(a | b) # Output: 7
# Bitwise XOR
print(a ^ b) # Output: 6
# Left Shift
print(a << 1) # Output: 10 (a * 2)
# Right Shift
print(a >> 1) # Output: 2 (a // 2)
18. Operator Precedence:
The precedence is the same as in C++:
()
>**
>*, /, //, %
>+,-
><<, >>
>&
>^
>|
>and
>or
.
19. Scope:
In Python, local scope refers to variables defined inside a function or block, and global scope refers to variables defined outside all functions. Local variables cannot be accessed outside their function, while global variables can be accessed anywhere in the code.
20. Data Type Modifiers in Python:
Python does not have explicit modifiers like long
or unsigned
:
int
in Python can handle large numbers (arbitrary precision).float
is used for decimal numbers.
Example:
x = 1000 # No need for 'long' as in C++
y = 10.5 # For floating point numbers
Python Projects
Data visualization using MatPlotLib
Sending Emails using API in python
Running Linux Commands in python
Sending Emails using API in python
Step1→ Install SMTP library (simple mail transfer protocol)
step2 → import that package
step3→ use the code as below
import smtplib
hostname = 'smtp.gmail.com'
email = 'Your mail id'
password = 'password'
with smtplib.SMTP(host=hostname, port=587) as connection:
connection.starttls()
connection.login(user=email,password=password)
connection.sendmail(
from_addr='the email address from which u r sending',
to_addrs='the email address to whom you want to send',
msg=f'subject:My Test Mail\n\n Hi this is my first test mail using python'
)
step4 →How to Generate an App Password for Google Account:
Login to our Google Account:
Go to Google Account
Enter your credentials to log in.
Enable Two-Step Verification:
Once logged in, click on Security in the left-hand menu.
Scroll down to the "Signing in to Google" section.
Click on 2-Step Verification and follow the instructions to enable it.
Generate an App Password:
After enabling two-step verification, go back to the Security section.
In the same "Signing in to Google" section, click on App passwords.
You may be asked to log in again for security purposes.
Under "Select app", choose Other (Custom name), then enter something descriptive (e.g., "Python Script").
Click Generate to create a unique app password.
Use the Generated App Password:
Copy the 16-character app password that appears.
You can now use this app password in your Python script (or any application) in place of your regular Google account password.
step4→ You have successfully sent an email using python script
Running Linux Commands in python
ls -a
ls -lt
python linux.py
If you want to run linux commands using python create a python file in linux and write as below:
import subprocess # used to run linux commands
subprocess.run('ls -a', shell=True)
subprocess.run('ls -ltr', shell=True)