18/06/2025, 23:19 Basics
In [1]:
name = "John" # String
age = 25 # Integer
height = 5.9 # Float
is_student = True # Boolean
print(name, age, height, is_student)
John 25 5.9 True
In [2]:
name = input("Enter your name: ")
print("Hello", name)
Enter your name: John
Hello John
In [3]:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult.")
else:
print("You are a minor.")
Enter your age: 17
You are a minor.
In [4]:
for i in range(5):
print("Loop number:", i)
Loop number: 0
Loop number: 1
Loop number: 2
Loop number: 3
Loop number: 4
In [5]:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
apple
banana
cherry
In [6]:
a = 10
b = 5
sum = a + b
print("Sum is:", sum)
Sum is: 15
In [7]:
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
Enter a number: 6
Even number
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 1/8
18/06/2025, 23:19 Basics
In [8]:
colors = ["red", "green", "blue"]
for color in colors:
print("Color:", color)
Color: red
Color: green
Color: blue
In [9]:
for i in range(1, 11):
print("3 x", i, "=", 3 * i)
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
In [10]:
a = 45
b = 78
c = 32
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
Largest: 78
In [11]:
text = "education"
vowels = "aeiou"
count = 0
for char in text:
if char in vowels:
count += 1
print("Number of vowels in", text, "is", count)
Number of vowels in education is 5
In [12]:
x = 10
print("Initial x:", x)
x += 5 # x = 10 + 5
print("After x += 5:", x)
x -= 3 # x = 15 - 3
print("After x -= 3:", x)
x *= 2 # x = 12 * 2
print("After x *= 2:", x)
x /= 4 # x = 24 / 4
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 2/8
18/06/2025, 23:19 Basics
print("After x /= 4:", x)
x %= 5 # x = 6 % 5
print("After x %= 5:", x)
x **= 3 # x = 1 ** 3
print("After x **= 3:", x)
x //= 2 # x = 1 // 2
print("After x //= 2:", x)
Initial x: 10
After x += 5: 15
After x -= 3: 12
After x *= 2: 24
After x /= 4: 6.0
After x %= 5: 1.0
After x **= 3: 1.0
After x //= 2: 0.0
In [13]:
for i in range(5): # 0 to 4
print(i)
0
1
2
3
4
In [14]:
for i in range(1, 6): # Start at 1, end before 6
print(i)
1
2
3
4
5
In [15]:
for i in range(2, 11, 2): # Step of 2
print(i)
2
4
6
8
10
In [16]:
for i in range(5, 0, -1): # Start at 5, end before 0, step -1
print(i)
5
4
3
2
1
In [17]:
for i in range(3, 31, 3):
print(i)
3
6
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 3/8
18/06/2025, 23:19 Basics
9
12
15
18
21
24
27
30
In [18]:
for i in range(1, 11):
print("Natural number:", i)
Natural number: 1
Natural number: 2
Natural number: 3
Natural number: 4
Natural number: 5
Natural number: 6
Natural number: 7
Natural number: 8
Natural number: 9
Natural number: 10
In [19]:
for i in range(1, 6):
print("Square of", i, "is", i * i)
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
In [20]:
for i in range(1, 6):
print("*" * i)
*
**
***
****
*****
In [21]:
numbers = [1, 2, 3, 4, 5]
for num in numbers:
print("Square of", num, "is", num ** 2)
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
In [22]:
nums = [10, 20, 30, 40]
total = 0
for num in nums:
total += num
print("Total sum:", total)
Total sum: 100
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 4/8
18/06/2025, 23:19 Basics
In [23]:
even_numbers = list(range(2, 21, 2))
print("Even numbers:", even_numbers)
Even numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
In [24]:
squares = []
for i in range(1, 6):
squares.append(i ** 2)
print("Squares:", squares)
Squares: [1, 4, 9, 16, 25]
In [25]:
marks = [45, 67, 89, 34, 76]
for mark in marks:
if mark > 50:
print("Pass:", mark)
Pass: 67
Pass: 89
Pass: 76
In [26]:
names = ["Tom", "Jerry", "Spike"]
for i in range(len(names)):
print("Index:", i, "Name:", names[i])
Index: 0 Name: Tom
Index: 1 Name: Jerry
Index: 2 Name: Spike
In [27]:
multiples = []
for i in range(1, 11):
multiples.append(i * 3)
print("Multiples of 3:", multiples)
Multiples of 3: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
In [28]:
fruits = ["apple", "banana", "cherry", "mango"]
fruits.reverse()
print("Reversed list:", fruits)
Reversed list: ['mango', 'cherry', 'banana', 'apple']
In [29]:
numbers = [10, 20, 30, 40, 50, 60]
for i in range(0, len(numbers), 2): # Step = 2
print("Jumped item:", numbers[i])
Jumped item: 10
Jumped item: 30
Jumped item: 50
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 5/8
18/06/2025, 23:19 Basics
In [30]:
fruits = ["apple", "banana"]
fruits.append("orange")
print(fruits)
['apple', 'banana', 'orange']
In [31]:
fruits.insert(1, "grapes") # Insert at index 1
print(fruits)
['apple', 'grapes', 'banana', 'orange']
In [32]:
fruits = ["apple", "banana", "orange"]
fruits.remove("banana")
print(fruits)
['apple', 'orange']
In [33]:
del fruits[1]
print(fruits)
['apple']
In [34]:
last_fruit = fruits.pop() # Removes last item
print("Removed:", last_fruit)
print(fruits)
Removed: apple
[]
In [35]:
fruits = ["apple", "banana", "cherry"]
if "banana" in fruits:
print("Yes, banana is in the list")
else:
print("No, banana not found")
Yes, banana is in the list
In [36]:
marks = [10, 20, 10, 30, 10]
print("Count of 10:", marks.count(10))
Count of 10: 3
In [37]:
names = ["Alice", "Bob", "Charlie"]
for i in range(len(names)):
print(f"Index {i} = {names[i]}")
Index 0 = Alice
Index 1 = Bob
Index 2 = Charlie
In [38]:
fruits = ["apple", "banana", "cherry"]
fruits.clear()
print("List after clearing:", fruits)
List after clearing: []
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 6/8
18/06/2025, 23:19 Basics
In [39]:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined = list1 + list2
print("Combined list:", combined)
Combined list: [1, 2, 3, 4, 5, 6]
In [40]:
text = "Hello, World"
print(text[0:5]) # Hello
print(text[-5:]) # World
print(text[::2]) # Hlo ol
Hello
World
Hlo ol
In [41]:
word = "Python"
for char in word:
print(char)
P
y
t
h
o
n
In [42]:
text = "Hello Python"
print(text.upper()) # HELLO PYTHON
print(text.lower()) # hello python
HELLO PYTHON
hello python
In [43]:
text = "Techstack"
print("Length:", len(text)) # 9
Length: 9
In [44]:
text = "Python is fun"
print("Python" in text) # True
print("Java" not in text) # True
True
True
In [45]:
text = "I love Java"
new_text = text.replace("Java", "Python")
print(new_text)
I love Python
In [46]:
first = "Hello"
second = "World"
combined = first + " " + second
print(combined)
Hello World
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 7/8
18/06/2025, 23:19 Basics
In [47]:
sentence = "banana"
print("Count of a:", sentence.count('a')) # 3
Count of a: 3
In [48]:
text = "Python"
print("Reversed:", text[::-1]) # nohtyP
Reversed: nohtyP
In [49]:
text = " Hello World "
print("Trimmed:", text.strip()) # Removes spaces from both ends
Trimmed: Hello World
In [50]:
text = "Python is amazing"
words = text.split() # By space by default
print(words) # ['Python', 'is', 'amazing']
['Python', 'is', 'amazing']
In [51]:
words = ['Python', 'is', 'awesome']
sentence = " ".join(words)
print(sentence)
Python is awesome
In [53]:
x = "1234"
y = "abc"
print(x.isdigit()) # True
print(y.isalpha()) # True
True
True
In [ ]:
localhost:8888/nbconvert/html/Python Basics/Basics.ipynb?download=false 8/8