Welcome to this beginner-friendly guide to Python programming! This README provides a foundational overview of key Python concepts.
Python is a high-level, interpreted, general-purpose programming language. It is known for its simplicity, readability, and broad community support.
- Easy to learn and write
- Interpreted and dynamically typed
- Extensive standard library
- Cross-platform
- Open-source
print("Hello, World!")A module is a file containing Python definitions and functions. Modules help organize code into manageable parts.
import math
print(math.sqrt(25)) # Outputs: 5.0Create a file mymodule.py:
def greet(name):
return f"Hello, {name}!"Use it in another file:
import mymodule
print(mymodule.greet("Prashant"))A variable is a name that references a value. Python variables are created when you assign a value.
x = 10
name = "Prashant"
is_active = True- Python is dynamically typed β you donβt need to declare the type.
- Must start with a letter or underscore
- Can contain letters, numbers, underscores
- Case-sensitive (
ageβAge)
- Separate words with underscore ("_").
- Constant variables should be BLOCK TYPED.
Data Type determines the nature of the data, It also determines the amount of space required to store the value.Python has several built-in data types categorized as follows:
| Primitive | Non-primitive |
|---|---|
int |
list |
float |
tuple |
str |
set" |
bool |
dictionary |
| Type | Example |
|---|---|
int |
10 |
float |
3.14 |
str |
"Hello" |
bool |
True, False |
| Type | Description | Example |
|---|---|---|
list |
Ordered, mutable sequence | [1, 2, 3] |
tuple |
Ordered, immutable sequence | (1, 2, 3) |
set |
Unordered, no duplicates | {1, 2, 3} |
dict |
Key-value pairs (hashmap) | {"name": "Prashant", "age": 17} |
x = 10
print(type(x)) # <class 'int'>Python provides many built-in methods for manipulating strings. Strings are immutable, so most methods return a new string.
| Method | Description | Example |
|---|---|---|
str.lower() |
Converts all characters to lowercase | "HELLO".lower() β "hello" |
str.upper() |
Converts all characters to uppercase | "hello".upper() β "HELLO" |
str.strip() |
Removes leading/trailing whitespace | " hello ".strip() β "hello" |
str.replace(a, b) |
Replaces substring a with b |
"Hello".replace("H", "J") β "Jello" |
str.find(sub) |
Returns index of first occurrence of sub |
"hello".find("e") β 1 |
str.count(sub) |
Counts number of times sub appears |
"hello".count("l") β 2 |
str.startswith() |
Checks if string starts with given value | "hello".startswith("he") β True |
str.endswith() |
Checks if string ends with given value | "hello".endswith("lo") β True |
str.split(sep) |
Splits string into a list by sep |
"a,b,c".split(",") β ["a", "b", "c"] |
str.join(list) |
Joins elements of list into a string | ",".join(["a", "b", "c"]) β "a,b,c" |
name = "Prashant"
age = 17
# f-string
print(f"My name is {name} and I'm {age} years old.")
# .format() method
print("My name is {} and I'm {} years old.".format(name, age))
# π Python Operations, Operators, Slicing & Indexing
## π Python Arithmetic Operations
Used for basic mathematical calculations:
```python
a = 10
b = 3
print(a + b) # Addition
print(a - b) # Subtraction
print(a * b) # Multiplication
print(a / b) # Division
print(a % b) # Modulus
print(a ** b) # Exponentiation
print(a // b) # Floor DivisionUsed to compare two values:
a = 5
b = 10
print(a == b) # Equal to
print(a != b) # Not equal to
print(a > b) # Greater than
print(a < b) # Less than
print(a >= b) # Greater than or equal to
print(a <= b) # Less than or equal toUsed to combine conditional statements:
x = True
y = False
print(x and y) # Logical AND
print(x or y) # Logical OR
print(not x) # Logical NOTUsed to assign values to variables:
a = 10
a += 5 # a = a + 5
a -= 3
a *= 2
a /= 4
a %= 2
a **= 3
a //= 2Work on bits and perform bit-by-bit operations:
a = 5 # 0b0101
b = 3 # 0b0011
print(a & b) # AND
print(a | b) # OR
print(a ^ b) # XOR
print(~a) # NOT
print(a << 1) # Left shift
print(a >> 1) # Right shiftx = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # True
print(x is z) # False
print(x is not z) # Truemy_list = [1, 2, 3, 4]
print(2 in my_list) # True
print(5 not in my_list) # TrueIndexing gives you access to individual elements in a sequence (like strings, lists, tuples):
my_list = [10, 20, 30, 40, 50]
print(my_list[0]) # 10
print(my_list[-1]) # 50Slicing allows you to extract a portion of a list or string:
my_list = [0, 1, 2, 3, 4, 5, 6]
print(my_list[1:5]) # [1, 2, 3, 4]
print(my_list[:3]) # [0, 1, 2]
print(my_list[3:]) # [3, 4, 5, 6]
print(my_list[::2]) # [0, 2, 4, 6]
print(my_list[::-1]) # [6, 5, 4, 3, 2, 1, 0] (reverse list)Check phone_number_balance_handler.py
"Conditional statements allow a program to execute certain blocks of code or functions only if specified conditions are met. For example, using if to run a function when a condition is true, and else to run a different function when it's false.
a = "male"
if a == "male":
print("You are a male!!")
else:
print("You are a cute little girlie and feminine character.")
# returns "You are a male!!"Lets say in some cases, 3 or 4 options apply for the code. For example; we got extra 1 extra gender ie. transgender and people could choose "rather not say". Here comes else if statement countering the first if statement returns negative boolean value.
a = "prefer not to say"
if a == "male":
print("You are a male!!")
elif a == "transgender":
print("You are a transgender")
elif a == "female":
print("Just marry me alreadyπΉπΉπΉ")
else:
print("You're cooked. You chose rather not to express your gender. Such a gay thing to do.")
#returns "You're cooked. You chose rather not to express your gender. Such a gay thing to do."