Env files allow you to put your environment variables inside a file. You just create a new file called. env in your project and slap your variables in there on different lines. This env is used for storing your all id, password, keys all important credential so no on directly can get your data. Code Step 1: Create a virtual environment and make it active Step 2: pip install python-dotenv Step 3: Create a .env file Step 4: Create test.py in the same dir where you created .env file Now Check Code test.py from dotenv import load_dotenv load_dotenv() import os my_site = os.getenv("DOMAIN") print("Visit My website : {} ".format(my_site)) .env # a comment that will be ignored. DOMAIN= https://thepyuniverse.blogspot.com Now Run test.py Output: Visit My website : https://thepyuniverse.blogspot.com Screenshot : Quiz Level 1 Quiz Level 2 Request Me
Concept Generators are used to create iterators, but with a different approach. Generators are simple functions that return an iterable set of items, one at a time, in a special way. When an iteration over a set of items starts using the for a statement, the generator is run. Once the generator's function code reaches a "yield" statement, the generator yields its execution back to the for loop, returning a new value from the set. The generator function can generate as many values (possibly infinite) as it wants, yielding each one in its turn. Code def gen_fun(): yield 1 yield 2 yield 3 yield 4 yield 5 for i in gen_fun(): print(i) >>> 1 2 3 4 5 Screenshot : Quiz Level 1 Quiz Level 2 Request Me
Concept Operation 1: When we are trying to write on file by using Python we have 2 kinds of write mode. i) 'w' stands for write mode but in this mode, the old file will be deleted by new file ii) 'a' also stands for write mode but in this mode, new data will be appended with old file Operation 2: When we are reading data from the file we will be using read mode 'r Write # ['a', 'w'] f = open("demo.txt", 'a') data = input('Enter Some Data :') f.write(data) f.close() Read f = open("demo.txt", 'r') print(f.read()) [ --> Reading whole data ] print(f.readline()) [ --> Reading a single Line ] print(f.readlines()) [ --> Whole data will be devided by newline and convert it as a List] print(f.read(10)) [ --> Will read from postion to postion 10 from a file] f.close() Quiz Level 1 Quiz Level 2 Request Me
Comments
Post a Comment