" Python is a high level, interepted programming language that was created by Guido van Rossum in 1991. It is well-known for its simplicity, readbility and versatile. the python is very clean and easy to understand, making it an excellent choice for beginners in programming language."
name=SujitTomarprint(name)
#Sujit Tomar>>> print ("Sujit")
Sujit
>>> 500 * 2
1000
>>> Programming Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
Programming
NameError: name 'Programming' is not defined. Did you mean: 'quit'?
It means not variable define
>>> import os
>>> os.getcwd() 'C:\\Users\\PC\\OneDrive\\Documents\\Python'
>>> import abcfgf
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
import abcfgf
ModuleNotFoundError: No module named 'abcfgf'
Remember cdw Means :- Current Working Directory
ModuleNotFoundError: it means when you write wrong name of library or module then show this type error
>>> for s in "Sujit":
... print(s)
... S
u
j
i
t
>>> <-------------------------------------------------->
>>> for s in "Sujit"
File "<python-input-6>", line 1
for s in "Sujit"
^
SyntaxError: expected ':'
<-------------------------------------------------->
>>> for s in "Sujit":
... print(s)
... File "<python-input-0>", line 2
print(s)
^^^^^
IndentationError: expected an indented block after 'for' statement on line 1
>>> >>> it means you write wrong syntax in python
First you missed (:) because in for loop it is mandatory and Second you miss space (IndentationError) that is error
>>> import sys
>>> sys.platform()
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
sys.platform()
~~~~~~~~~~~~^^
TypeError: 'str' object is not callable
>>> >>> import sys
>>> sys.platform
'win32'
>>> TypeError: 'str' object is not callable :- Means it is not a function
"When you import allrady any page to another page then use below syntax for update"
>>> from importlib import reload
>>> reload(page)
| Data Type | Discripition | Type | Example |
|---|---|---|---|
| Integers | Immutable | x= 5, y = 10 | |
| Floting Point Numbers | Immutable | a = 3.14, b = 5.36 | |
| Booliean | Immutable | is_active = True, isCompleted = False | |
| String | Immutable | name = 'Sujit', fname = "bob's" | |
| Tuples | Immutable | cordinates = (10, 20) | |
| Frozen set | Immutable | frozen_set_example = frozenset([1, 2,3]) | |
| Bytes | Immutable | bytes_example = b"Sujit Tomar" | |
| List | Mutable | num = [, 2, 3] | |
| Set | Mutable | num = {1, 2, 3} | |
| Dictionary | Mutable | stu = {"name" = "Sujit", "age" = 22} | |
| Bitearray | Mutable | value = bytearray([65, 66, 67]) | |
| Array | it is contain same type value | Mutable | int_array = arrar.array("i", [1, 2, 3]) |
# this is commentsUse ** for for Exponentiation (Power Calculation):
# Squaring a numbersquare=5**2# Output: 25# Cubing a numbercube=3**3# Output: 27# Power of any numberresult=2**5# Output: 32Swapping Two Variables Without a Temporary Variable:
a=5b=10# Swap valuesa, b=b, aprint(a) # Output: 10print(b) # Output: 5List Comprehension :
# Create a list of squares from 0 to 9squares= [x**2forxinrange(10)]
print(squares) # Output: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]# Filter even numbers from a listeven_numbers= [xforxinrange(20) ifx%2==0]
print(even_numbers) # Output: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]Unpacking Elements in a List:
numbers= [1, 2, 3, 4]
# Unpack into variablesa, b, c, d=numbersprint(a, b, c, d) # Output: 1 2 3 4# Unpacking with *a, *b, c=numbersprint(a, b, c) # Output: 1 [2, 3] 4Using enumerate() for Index and Value:
fruits= ['apple', 'banana', 'cherry']
forindex, fruitinenumerate(fruits):
print(index, fruit)
# Output:# 0 apple# 1 banana# 2 cherryUsing zip() for Parallel Iteration:
names= ['Alice', 'Bob', 'Charlie']
scores= [85, 90, 95]
forname, scoreinzip(names, scores):
print(f'{name}: {score}')
# Output:# Alice: 85# Bob: 90# Charlie: 95Ternary Conditional Expression:
# Conditional assignmentx=10y=20max_value=xifx>yelseyprint(max_value) # Output: 20Using set() for Removing Duplicates from a List:
numbers= [1, 2, 2, 3, 4, 4, 5]
unique_numbers=list(set(numbers))
print(unique_numbers) # Output: [1, 2, 3, 4, 5]Reversing a String or List:
# Reverse a stringtext="hello"reversed_text=text[::-1]
print(reversed_text) # Output: "olleh"# Reverse a listmy_list= [1, 2, 3, 4, 5]
reversed_list=my_list[::-1]
print(reversed_list) # Output: [5, 4, 3, 2, 1]Reversing a String or List:
# Reverse a stringtext="hello"reversed_text=text[::-1]
print(reversed_text) # Output: "olleh"# Reverse a listmy_list= [1, 2, 3, 4, 5]
reversed_list=my_list[::-1]
print(reversed_list) # Output: [5, 4, 3, 2, 1]Using _ in the REPL to Access the Last Result:
>>>10+515>>> _ *230Chaining Comparison Operators:
# Check if a number is between 10 and 20x=15print(10<x<20) # Output: TrueMerging Two Dictionaries (Python 3.9+):
dict1= {'a': 1, 'b': 2}
dict2= {'c': 3, 'd': 4}
merged_dict=dict1|dict2print(merged_dict) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}Using any() and all() for Logical Checks:
# Check if any number is positivenumbers= [-1, -2, 3, -4]
print(any(n>0forninnumbers)) # Output: True# Check if all numbers are positiveprint(all(n>0forninnumbers)) # Output: FalseSorting a List of Dictionaries by Key:
students= [
{'name': 'Alice', 'grade': 90},
{'name': 'Bob', 'grade': 85},
{'name': 'Charlie', 'grade': 95}
]
# Sort by 'grade'sorted_students=sorted(students, key=lambdastudent: student['grade'])
print(sorted_students)
# Output: [{'name': 'Bob', 'grade': 85}, {'name': 'Alice', 'grade': 90}, {'name': 'Charlie', 'grade': 95}]Using defaultdict to Avoid KeyErrors:
fromcollectionsimportdefaultdict# Create a defaultdict with default type 'int'my_dict=defaultdict(int)
my_dict['apple'] +=1print(my_dict) # Output: defaultdict(<class 'int'>, {'apple': 1})Combining Multiple Strings:
words= ["Python", "is", "fun"]
sentence=" ".join(words)
print(sentence) # Output: Python is funLambda Functions:
# A simple lambda function to calculate squaresquare=lambdax: x**2print(square(5)) # Output: 25# Using lambda with `map`numbers= [1, 2, 3, 4]
squares=list(map(lambdax: x**2, numbers))
print(squares) # Output: [1, 4, 9, 16]Using Counter to Count Elements in a List:
fromcollectionsimportCounterelements= ['a', 'b', 'a', 'c', 'b', 'a']
counter=Counter(elements)
print(counter) # Output: Counter({'a': 3, 'b': 2, 'c': 1})Dictionary Comprehension:
# Example: Create a dictionary of squaressquares_dict= {x: x**2forxinrange(6)}
print(squares_dict) # Output: {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}Using get() to Access Dictionary Values Safely:
my_dict= {'a': 1, 'b': 2}
# Access an existing keyprint(my_dict.get('a')) # Output: 1# Access a non-existing key (returns None)print(my_dict.get('c')) # Output: None# Providing a default valueprint(my_dict.get('c', 0)) # Output: 0math :
>>> import math
>>> math.pi
3.141592653589793
>>> Random :
>>> import random
>>> random.random()
0.4596604027751191
>>> random.random()
0.20735437958994385
>>> random.random()
0.39390716757344313
>>> random.random()
0.9018445517995602
>>> >>> random.choice([1, 2, 3, 4, 5])
3
>>> random.choice([1, 2, 3, 4, 5])
4
>>> random.choice([1, 2, 3, 4, 5])
3
>>> Range :
>>> squar = [x**2 for x in range(10)]
>>> squar
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> cubeNum = [x**3 for x in range (10)]
>>> cubeNum
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
>>>