Jun 21, 2024
virtualenv env_name
env_name\Scripts\activate.ps1
deactivate
env1
, env2
) and install different packages (pandas
, pyjokes
), then freeze dependencies to requirements.txt
with pip freeze > requirements.txt
and install with pip install -r requirements.txt
.lambda arguments: expression
square = lambda n: n * n
for squaring a number.separator.join(iterable)
'-'.join(['Harry', 'Rohan', 'Shubham'])
→ Harry-Rohan-Shubham
.{}
for position-based formatting.",".format(name, marks, phone)
map(function, iterable)
map(lambda x: x*x, [1, 2, 3, 4])
filter(function, iterable)
filter(lambda x: x % 2 == 0, [1, 2, 3, 4])
reduce(function, iterable)
from functools
reduce(lambda x, y: x + y, [1, 2, 3, 4])
try:
# code that might raise an exception
except SomeException as e:
# code that runs if an exception occurs
else:
# code that runs if no exception occurs
finally:
# code that always runs (clean-up code)
if __name__ == "__main__":
if __name__ == "__main__":
main()
global var_name
a = 10
def update():
global a
a = 20
update()
print(a) # 20
[expression for item in iterable]
[x*x for x in range(10)]
**
unpacking to merge.with
dict1 = {'a': 1}
dict2 = {'b': 2}
merged_dict = {**dict1, **dict2}
with open('file1.txt') as f1, open('file2.txt') as f2:
...
I hope these notes help you in effectively understanding and using advanced Python concepts!
requirements.txt
Commands:
pip install virtualenv
virtualenv env1
source env1/bin/activate # For Unix-based systems
.env1\Scripts\activate.ps1 # For Windows
pip install pandas pyjokes
pip freeze > requirements.txt
pip install -r requirements.txt
format
methodname = input('Enter name: ')
marks = int(input('Enter marks: '))
phone = input('Enter phone number: ')
formatted_string = "Name: {}, Marks: {}, Phone: {}".format(name, marks, phone)
print(formatted_string)
table = [7 * i for i in range(1, 11)]
sequence = '\n'.join(map(str, table))
print(sequence)
def divisible_by_5(x):
return x % 5 == 0
l = [1, 2, 10, 3, 15, 25]
filtered_list = list(filter(divisible_by_5, l))
print(filtered_list)
from functools import reduce
def max_reduce(a, b):
return a if a > b else b
numbers = [1, 2, 5, 6, 3]
max_number = reduce(max_reduce, numbers)
print(f'The maximum number is: {max_number}')
pip freeze
to export and recreate environmentCommands:
pip freeze > requirements.txt
pip install -r requirements.txt
Flask
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello, World!"
if __name__ == "__main__":
app.run()
I hope these practice sets help you in applying advanced Python concepts efficiently! 😊
😎 Have a great time coding!
Happy Learning!
Source: Lecture/Presentation on Advanced Python Concepts.