This guide covers fundamental Python concepts with examples. Each section includes code samples and explanations.
Basic "Hello World" program implementation can be found in 01-hello-world.py
.
When working with strings containing apostrophes, you have two options:
- Using escape characters:
# Using escape character
text = 'Viswa\'s World'
- Using double quotes (recommended):
# Better approach
text = "Viswa's World"
Python supports multiline strings using triple double quotes:
"""Viswa's World was fantastic
in the 1990's"""
message = "Hello World"
print(len(message)) # Get string length
message = "Hello World"
print(message[10]) # Outputs: 'd'
message = "Hello World"
print(message[0:5]) # Outputs: 'Hello'
message = "Hello World"
print(message.upper()) # Convert to uppercase
print(message.lower()) # Convert to lowercase
message = "Hello World"
print(message.count('l')) # Outputs: 3 (counts occurrences of 'l')
print(message.find('World')) # Outputs: 6 (index where 'World' starts)
message = "Hello World"
message = message.replace('World', 'Python')
print(message) # Outputs: 'Hello Python'
greeting = "Hello"
name = "Viswa"
message = greeting + ', ' + name + '. Welcome'
# Outputs: 'Hello, Viswa. Welcome'
greeting = "Hello"
name = "Viswa"
message = '{}, {}. Welcome!'.format(greeting, name)
# Outputs: 'Hello, Viswa. Welcome!'
greeting = "Hello"
name = "Viswa"
message = f'{greeting}, {name.upper()}. Welcome!'
# Outputs: 'Hello, VISWA. Welcome!'
- Use
dir()
function to view all available string methods and attributes - Methods are similar to functions but are associated with objects
Note: This guide is continuously updated with new Python concepts and examples.
greeting = "Hello" name = "Viswa" print(dir(name))
Output:
['add', 'class', 'contains', 'delattr', 'dir', 'doc', 'eq', 'format', 'ge',] etc.
greeting = "Hello" name = "Viswa" print(help(str.lower))
Output:
lower(self, /)
Return a copy of the string converted to lowercase.
- Addition: 3 + 2
- Subtraction: 3 - 2
- Multiplication: 3 * 2
- Division: 3 / 2
- Floor Division: 3 // 2
- Exponent: 3 ** 2
- Modulus: 3 % 2
- Equal: 3 == 2
- Not Equal 3 != 2
- Greater Than: 3 > 2
- Less Than: 3 < 2
- Greater than or Equal: 3 >= 2
- Less or Equal: 3 <= 2
lists are mutable in python. we can add, remove and change the position etc.
courses = ['history', 'physics', 'maths', 'science']
print(courses)
Output:
['history', 'physics', 'maths', 'science']
print(len(courses))
Output:
4
print(courses[2])
Output:
maths
print(courses[-1])
Output:
science
print(courses[4])
Output:
IndexError: list index out of range
print(courses[0:2])
Output:
['history', 'physics']
print the range of values i.e (it's should be assume it's starts from begining same result as above)
print(courses[:2])
Output:
['history', 'physics']
print(courses[2:])
Output:
['maths', 'science']
courses = ['history', 'physics', 'maths', 'science'] courses.append('arts') print(courses)
Output:
['history', 'physics', 'maths', 'science', 'arts']
courses = ['history', 'physics', 'maths', 'science'] courses.insert(0, 'arts') print(courses)
Output:
['arts', 'history', 'physics', 'maths', 'science']
courses = ['history', 'physics', 'maths', 'science'] courses_2 = ['arts', 'education']
courses.insert(0, courses_2) print(courses)
Output:
[['arts', 'education'], 'history', 'physics', 'maths', 'science']
courses = ['history', 'physics', 'maths', 'science'] courses_2 = ['arts', 'education']
courses.insert(0, courses_2) print(courses[0])
Output:
['arts', 'education']
courses = ['history', 'physics', 'maths', 'science'] courses_2 = ['arts', 'education']
courses.extend(courses_2) print(courses)
Output:
['history', 'physics', 'maths', 'science', 'arts', 'education']
courses = ['history', 'physics', 'maths', 'science'] courses.remove('maths') print(courses)
Output:
['history', 'physics', 'science']
courses = ['history', 'physics', 'maths', 'science'] courses.pop() print(courses)
Output:
['history', 'physics', 'maths']
courses = ['history', 'physics', 'maths', 'science'] courses.reverse() print(courses)
Output:
['science', maths', 'physics', 'history',]
courses = ['history', 'physics', 'maths', 'science'] courses.sort() print(courses)
Output:
['history', 'maths', 'physics', 'science']
numbers = [1, 7, 3, 5, 2] numbers.sort() print(numbers)
Output:
[1, 2, 3, 5, 7]
Tuples are immutable in python. tuples are exactly the same as lists, but instaed of using squre braces [] use Parentheses ()
Example:
tuple_1 = ('history', 'physics', 'maths', 'science')
tuple_1 = ('history', 'physics', 'maths', 'science') tuple_2 = tuple_1
print(tuple_1) print(tuple_2)
Output:
('history', 'physics', 'maths', 'science')
('history', 'physics', 'maths', 'science')
tuple_1 = ('history', 'physics', 'maths', 'science') tuple_2 = tuple_1
print(tuple_1) print(tuple_2)
tuple_1[0] = 'art'
print(tuple_1) print(tuple_2)
Output:
TypeError: 'tuple' object does not support item assignment, beacuse tuple is immutable