py --version
python --version
# \n : Newline
# \t : Tab
# \\ : Backslash
# \" : Double quote
# \' : Single quot
print("Hello, World!\nThis is a new line.")
print("This is a tab:\tSee the space?")
print("This is a backslash: \\")
print("This is a double quote: \"")
print("This is a single quote: \'")
Message = 'Hello Python interpreter!'
print(Message.upper()) # HELLO PYTHON INTERPRETER!
print(Message.lower()) # hello python interpreter!
FirstName = 'abc'
LastName = 'xyz'
FullName = FirstName.title()+' '+LastName.title()
print(FullName) # Abc Xyz
print('abc xyz'.title()) # Abc Xyz
name =input("Enter your name: ")
age =int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old.")
print("Hello {0}, you are {1} years old.".format(name, age))
print("Hello %s, you are %d years old." % (name, age))
print("Hello {name}, you are {age} years old.".format(name=name, age=age))
print("Hello "+name, " you are "+str(age)+" years old.")
# print function enter new line By Default
print("Hello, World!") #Means print("Hello, World!",end='\n')
# print function without new line
print("Hello, World!", end='')
print("Hello, World2!")
#print function without separator
print("Hello", "World",5,6)
# print function with separator
print("Hello", "World", sep=', ')
# print function with multiple arguments
print("Hello", "World", "Python", sep=' - ', end='!\n')
print(' Hello hello '.strip()) # remove left right space
print(' Hello hello '.rstrip()) # remove right space
print(' Hello hello '.lstrip()) # remove left space
# removes the specified prefix ('https://') from the beginning of the string, if it exists.
print('https://nostarch.com'.removeprefix('https://'))
# removes the specified suffix ('.com') from the end of the string, if it exists.
print('https://nostarch.com'.removesuffix('.com'))
1. 7 / 2 → Floating-Point Division
- This is normal division.
- It returns a float (decimal value).
- So,
7 / 2 = 3.5
2. 7 // 2 → Floor Division
- This is integer (floor) division.
- It discards the decimal part and returns only the integer part of the division.
- So,
7 // 2 = 3(because 3.5 floored is 3)
# order=> not->and->or
# True or False and False # True or False=> True
# True and not False # True
# Membership Operators:
# 'in' and 'not in' are membership operators in Python.
# They are used to test if a value is found in a sequence (such as a string, list, or tuple).
# Example:
my_list =
print(3 in my_list) # Output: True
print(6 not in my_list) # Output: True
# Identity Operators:
# 'is' and 'is not' are identity operators in Python.
# They are used to test if two variables refer to the same object in memory.
a =
b = a
c =
print(a is b) # Output: True (b is the same object as a)
print(a is c) # Output: False (c is a different object with the same content)
print(a is not c) # Output: True (a and c are not the same object)
# match value:
# case pattern1:
# # Code to execute if value matches pattern1
# case pattern2:
# # Code to execute if value matches pattern2
# case _:
# # Default case (if no patterns match
Ststus = 404
match Ststus:
case 200:
print("OK")
case 404:
print("Not Found")
case 500:
print("Internal Server Error")
case _:
print("Unknown Status Code")
#Range range(10),range(1,10,2),list(range(1,20,2)) # (range(0, 10), range(1, 10, 2), )
for i in range(1,10):
if i == 5:
#continue: skip the rest of the loop for this iteration
continue
print(i)
if i == 8:
#break: exit the loop entirely
break
# This code will print numbers from 1 to 9, skipping 5 and stopping at 8.
# Output:
# 1
# 2
# 3
# 4
# 6
# 7
for i in range(1,10):
if i == 5:
pass #do nothing
print(i)
# Output:
# 1
# 2
# 3
# 4
# 6
# 7
# 8
# 9
a='0123456789'
print(a) # This will print '0'
print(a) # This will print '1234'
print(a) # This will print '123456789'
print(a) # This will print '13'
print(a) # This will print '02468'
print(a) # This will print '9876543210'
print(a) # This will print '9' #reversed indexing 10-1 = 9
print(a) # This will print '8' #reversed indexing 10-2 = 8
text = " hello world "
print(text.upper())
# Output: " HELLO WORLD "
print(text.lower())
# Output: " hello world "
print(text.strip())
# Output: "hello world"
print(text.replace("world", "Python")) # Output: " hello Python "
print(text.split()) #print(type(text.split())) return list
# Output:
text = "apple-banana-orange"
fruits = text.split('-')
print(fruits) # Output:
newText = '**'.join(fruits)
print(newText) # Output: apple**banana**orange
text = "hello world"
print(text.title()) # Output: "Hello World"
print(text.capitalize()) # Output: "Hello world"
text = "Python is fun"
print(text.find("is")) # Output: 7
print(text.replace("fun", "awesome")) # Output: "Python is awesome"
# Checking String Properties
text = "Python123"
print(text.isalpha()) # Output: False
print(text.isdigit()) # Output: False
print(text.isalnum()) # Output: True
print(text.isspace()) # Output: False
What is a Variable?
A variable in Python is like a container or label used to store data or information. You give it a name, assign it a value, and then use that name to access or change the value later.
📌 Example:
name = "Himanshu"
age = 25
pi = 3.14
Here,
namestores a string ("Himanshu")agestores an integer (25)pistores a float (3.14)
Variables are Dynamically Typed
Unlike other languages (like Java or C++), you don’t need to declare the data type in Python. It figures out the type based on the value you assign.
x = 100 # x is an integer
x = "Hello" # Now x is a string
Checking the Type of a Variable
You can use the built-in type() function to check the data type of a variable.
x = 42
print(type(x)) # Output: <class 'int'>
Multiple Assignments
Python allows you to assign values to multiple variables in a single line.
a = b = c = 0 # All three variables get the value 0
x, y, z = 1, 2, 3 # Each variable gets its own value
Data Types in Python
- Integers (int): Whole numbers (e.g., 10 , -5).
- Floats (float): Decimal numbers (e.g., 3.14 , -0.001).
- Strings (str): Text data enclosed in quotes (e.g., "Hello" , 'Python').
- Booleans (bool): Represents True or False .
- Lists: Ordered, mutable collections (e.g., ).
- Tuples: Ordered, immutable collections (e.g., (1, 2, 3)).
- Sets: Unordered collections of unique elements (e.g., {1, 2, 3}).
- Dictionaries: Key-value pairs (e.g., {"name": "Alice", "age": 25})
Constants:-
A constant is a variable whose value stays the same throughout the life of a program. Python doesn’t have built-in constant types,
but Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed:
MAX_CONNECTIONS = 5000
When you want to treat a variable as a constant in your code, write the name of the variable in all capital letters.