Complete_Python_Concepts_and_Examples
Complete_Python_Concepts_and_Examples
1. Membership Operator
2. Complex Number
z = 3 + 4j
print(z.real, z.imag)
# Arithmetic
print(5 + 2, 5 - 2, 5 * 2, 5 / 2)
# Relational
print(5 > 2, 5 == 2, 5 != 2)
a=5
b = float(a) # 5.0
c = str(a) # '5'
5. Python Applications
- ML: scikit-learn
- Automation: scripts
x = [1, 2]
y=x
Python Basics Summary
print(x is y) # Identity
print(2 in x) # Membership
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
a=5 # int
b = 2.5 # float
c = 2 + 3j # complex
d = Fraction(1, 3) # fraction
- Easy syntax
- Portable
- Large libraries
name = "Alice"
age = 25
print(type(name), type(age))
x = 10
name = "John"
x = 10 # int
y = 3.14 # float
s = "Hi" # str
l = [1, 2] # list
d = {"a": 1} # dict
- Arithmetic: + - * / % // **
print(100) # int
print(3.14) # float
1. Output of capitalize()
s = "123hello WORLD"
res = s.capitalize()
print(res)
2. Output of addToList()
def addToList(a):
a += [10]
addToList(b)
print(len(b))
- Advantages:
* No duplicates
- Disadvantages:
* Unordered
# Check EVEN/ODD
n=5
if n & 1:
print('Odd')
else:
print('Even')
a, b = 3, 4
a ^= b
b ^= a
a ^= b
print(a, b)
# Slicing Method
s = 'madam'
# 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
print(d)
8. input() vs print()
9. String Checks
s = 'apple'
print(s[0].lower() in 'aeiou')
# b) Contains word
word = 'world'
- count(): Frequency
a, b = True, False
Python Basics Summary
print(a or b) # True
print(not a) # False
x = '123'
13. Duplicate of Q1
x = 10
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]
s = 'hello'
Python Basics Summary
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)