'
) or double quotes ("
). They are immutable sequences of characters.# Example of creating strings
str1 = 'Hello, '
str2 = "world!"
+
), repetition (*
), and membership (in
operator).# Example of basic string operations
str1 = 'Hello, '
str2 = 'world!'
concatenated_str = str1 + str2
repeated_str = str1 * 3
print(concatenated_str) # Output: Hello, world!
print(repeated_str) # Output: Hello, Hello, Hello,
# Example of accessing characters in a string
my_string = 'Python'
print(my_string[0]) # Output: 'P'
print(my_string[3]) # Output: 'h'
string[start:end:step]
.# Example of string slicing
my_string = 'Python Programming'
print(my_string[7:]) # Output: 'Programming'
print(my_string[:6]) # Output: 'Python'
print(my_string[::2]) # Output: 'Pto rgamn'
upper()
, lower()
, strip()
, replace()
, split()
, etc.# Example of string methods
my_string = ' Hello, World! '
print(my_string.strip()) # Output: 'Hello, World!'
print(my_string.lower()) # Output: ' hello, world! '
print(my_string.replace('Hello', 'Hi')) # Output: ' Hi, World! '
str.format()
, and %
formatting.# Example of string formatting
name = 'Alice'
age = 25
print(f'My name is {name} and I am {age} years old.')
# Output: 'My name is Alice and I am 25 years old.'
# Example of creating lists
empty_list = []
numbers = [1, 2, 3, 4, 5]
mixed_list = [1, "hello", 3.14, True]
+
), repetition (*
), and membership (in
).# Example of basic list operations
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
repeated_list = list1 * 3
print(concatenated_list) # Output: [1, 2, 3, 4, 5, 6]
print(repeated_list) # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]
list[start:end:step]
syntax.# Example of indexing and slicing in lists
my_list = [10, 20, 30, 40, 50]
# Accessing individual elements
print(my_list[0]) # Output: 10
print(my_list[2]) # Output: 30
# Slicing
print(my_list[1:4]) # Output: [20, 30, 40]
print(my_list[::2]) # Output: [10, 30, 50]
len()
, max()
, min()
, sum()
, and sorted()
.# Example of built-in functions used on lists
numbers = [10, 5, 8, 3, 7]
print(len(numbers)) # Output: 5 (length of the list)
print(max(numbers)) # Output: 10 (maximum element)
print(min(numbers)) # Output: 3 (minimum element)
print(sum(numbers)) # Output: 33 (sum of elements)
print(sorted(numbers)) # Output: [3, 5, 7, 8, 10] (sorted list)
append()
, extend()
, insert()
, remove()
, pop()
, reverse()
, and sort()
.# Example of list methods
my_list = [10, 20, 30]
my_list.append(40) # Adds 40 to the end of the list
print(my_list) # Output: [10, 20, 30, 40]
my_list.insert(1, 15) # Inserts 15 at index 1
print(my_list) # Output: [10, 15, 20, 30, 40]
my_list.remove(20) # Removes the first occurrence of 20
print(my_list) # Output: [10, 15, 30, 40]
popped_item = my_list.pop() # Removes and returns the last item
print(popped_item) # Output: 40
print(my_list) # Output: [10, 15, 30]
my_list.reverse() # Reverses the list
print(my_list) # Output: [30, 15, 10]
my_list.sort() # Sorts the list in ascending order
print(my_list) # Output: [10, 15, 30]
del
Statementdel
statement is used to delete elements from a list or even delete the entire list. You can delete a specific element by its index or slice.# Example of del statement
my_list = [10, 20, 30, 40, 50]
del my_list[1] # Deletes the element at index 1
print(my_list) # Output: [10, 30, 40, 50]
del my_list[1:3] # Deletes elements from index 1 to 2 (slice)
print(my_list) # Output: [10, 50]
del my_list # Deletes the entire list
print()
, len()
, type()
, range()
, input()
, etc. These functions are always available and don't require any imports.# Example of built-in functions
print("Hello, World!") # Output: Hello, World!
numbers = [1, 2, 3, 4, 5]
print(len(numbers)) # Output: 5
print(type(numbers)) # Output: <class 'list'>
Python provides a vast standard library with built-in modules that you can import for extended functionality. Some commonly used modules are:
math
: Mathematical operations.
random
: Generating random numbers.
datetime
: Working with dates and times.
os
: Interfacing with the operating system.
# Example of using commonly used modules
import math
import random
import datetime
print(math.sqrt(16)) # Output: 4.0
print(random.randint(1, 10)) # Output: A random number between 1 and 10
print(datetime.datetime.now()) # Output: Current date and time
def
keyword, followed by the function name and parentheses. A function can be called by using its name followed by parentheses.# Example of defining and calling a function
def greet(name):
print(f"Hello, {name}!")
greet("Alice") # Output: Hello, Alice!
return
statement. If no return statement is present, or the function returns None
, it's considered a "void function".# Example of function with return statement
def add(a, b):
return a + b
result = add(5, 3)
print(result) # Output: 8
# Example of a void function
def print_message():
print("This is a void function")
print_message() # Output: This is a void function
global
keyword.# Example of variable scope and lifetime
def my_function():
local_var = 10 # Local variable
print(local_var)
global_var = 20 # Global variable
my_function() # Output: 10
print(global_var) # Output: 20
# Modifying a global variable inside a function
def modify_global():
global global_var
global_var = 50
modify_global()
print(global_var) # Output: 50
# Example of default parameters
def greet(name="User"):
print(f"Hello, {name}!")
greet() # Output: Hello, User!
greet("Alice") # Output: Hello, Alice!
# Example of keyword arguments
def display_info(name, age):
print(f"Name: {name}, Age: {age}")
display_info(age=25, name="Bob") # Output: Name: Bob, Age: 25
sys
module. The sys.argv
list stores these arguments.# Example of command-line arguments
import sys
def print_arguments():
print("Command Line Arguments:", sys.argv)
# To run this, you would typically use a command like:
# python script.py arg1 arg2 arg3
Made By SOU Student for SOU Students