Python Basics Summary
1. Membership Operator
fruits = ['apple', 'banana']
print('banana' in fruits) # True
2. Complex Number
z = 3 + 4j
print(z.real, z.imag)
3. Arithmetic vs Relational Operators
# Arithmetic
print(5 + 2, 5 - 2, 5 * 2, 5 / 2)
# Relational
print(5 > 2, 5 == 2, 5 != 2)
4. Data Types & Conversions
a=5
b = float(a) # 5.0
c = str(a) # '5'
5. Python Applications
- Web: Flask, Django
- Data Science: pandas, numpy
- ML: scikit-learn
- Automation: scripts
6. Identity vs Membership vs Logical
x = [1, 2]
y=x
Python Basics Summary
print(x is y) # Identity
print(2 in x) # Membership
print(1 in x and 3 in x) # Logical
7. Python Applications
- Web apps
- Data analysis
8. What is Anaconda?
A Python distribution for data science. Comes with Jupyter, Spyder, etc.
9. Numeric Types
from fractions import Fraction
a=5 # int
b = 2.5 # float
c = 2 + 3j # complex
d = Fraction(1, 3) # fraction
10. Key Features of Python
- Easy syntax
- Portable
- Large libraries
- Used in web, data science, ML
11. Installation Steps
1. Python: Download from python.org, install.
2. Anaconda: Download from anaconda.com, install.
3. Spyder: Open from Anaconda Navigator.
Python Basics Summary
12. Variables & Data Types
name = "Alice"
age = 25
print(type(name), type(age))
13. Spyder vs Other IDEs
- Scientific tools built-in
- Targeted for data science
14. Define Variable
x = 10
name = "John"
15. What is Python?
A high-level, readable language used for web, ML, data science.
16. Data Types Examples
x = 10 # int
y = 3.14 # float
s = "Hi" # str
l = [1, 2] # list
d = {"a": 1} # dict
17. Basic Operators
- Arithmetic: + - * / % // **
- Relational: == != > <
- Logical: and or not
- Membership: in, not in
- Identity: is, is not
Python Basics Summary
18. Numeric Types & Use
from decimal import Decimal
print(100) # int
print(3.14) # float
print(2 + 3j) # complex
print(Decimal('0.1')) # precise calc
1. Output of capitalize()
s = "123hello WORLD"
res = s.capitalize()
print(res)
# Output: "123hello world" (first letter capital, rest lowercase)
2. Output of addToList()
def addToList(a):
a += [10]
b = [10, 20, 30, 40]
addToList(b)
print(len(b))
# Output: 5 (list is modified in-place)
3. Sets: Advantages & Disadvantages
- Advantages:
* No duplicates
* Fast membership testing
* Useful for mathematical operations
- Disadvantages:
* Unordered
* Can't access items by index
Python Basics Summary
4. Bitwise Operation Code
# Check EVEN/ODD
n=5
if n & 1:
print('Odd')
else:
print('Even')
# Swap using XOR
a, b = 3, 4
a ^= b
b ^= a
a ^= b
print(a, b)
5. Symmetrical or Palindrome Check
# Slicing Method
s = 'madam'
print(s == s[::-1]) # Palindrome
# Naïve Method
for i in range(len(s)//2):
if s[i] != s[-(i+1)]:
print('Not Palindrome')
break
else:
print('Palindrome')
6. Casting a Variable
a = '5'
Python Basics Summary
b = int(a) # Now b is 5 (int)
7. Output of dict with duplicate keys
d = {'a': 1, 'b': 2, 'a': 3}
print(d)
# Output: {'a': 3, 'b': 2}
8. input() vs print()
- input(): Takes input from user
- print(): Displays output to user
9. String Checks
# a) Starts with vowel
s = 'apple'
print(s[0].lower() in 'aeiou')
# b) Contains word
word = 'world'
print('world' in 'Hello world')
10. List Method Differences
- append(): Adds one item
- extend(): Adds multiple items
- pop(): Removes by index
- remove(): Removes by value
- count(): Frequency
- index(): First index of value
11. Boolean Operators
a, b = True, False
Python Basics Summary
print(a or b) # True
print(a and b) # False
print(not a) # False
12. Type Casting
x = '123'
y = int(x) # y becomes 123
13. Duplicate of Q1
Same as Q1. Output: "123hello world"
14. type() Function
x = 10
print(type(x)) # <class 'int'>
15. List vs Tuple vs Set
- List: [1, 2], mutable
- Tuple: (1, 2), immutable
- Set: {1, 2}, unordered, unique
16. List Slicing
s = [1, 2, 3, 4, 5, 6]
print(s[::-1]) # [6, 5, 4, 3, 2, 1]
print(s[::2]) # [1, 3, 5]
print(s[::2][::-1]) # [5, 3, 1]
print(s[4:0:-1]) # [5, 4, 3, 2]
17. String vs List (Mutability)
s = 'hello'
Python Basics Summary
# s[0] = 'H' => Error (immutable)
l = ['h', 'e', 'l']
l[0] = 'H' # Works
18. Count Vowels and Consonants
s = 'Hello World'
v, c = 0, 0
for ch in s.lower():
if ch.isalpha():
if ch in 'aeiou':
v += 1
else:
c += 1
print('Vowels:', v, 'Consonants:', c)