python get environment variable example code

Here our topic is, python get environment variable, in simple how to get value of environment variable in python .so we should cover how to set environment variable and using python get environment variable value
To work with environment variables in Python is the same like working with dictionaries. first you need to import the os module.

import os

/* To get the value of all environment variable value in python*/




/* To set the environment variable value by name in python */

# To Set environment variables in python

os.environ['EMAIL'] = '[email protected]' 

os.environ['USER_ID'] = '4568'

/* To get the environment variable value by name in python */


# To get environment variables in python

import os
USER_EMAIL = os.environ.get('EMAIL') 
USER_ID = os.environ.get('USER_ID') 

python if else in one line like ternary operator

Being a programmer we heard about conditional expressions ,an expression that give you true or false result based on a condition ,here in python how we are going to deal with  ‘  python if else in one line  ‘ ,so let we check how to create an if else statement in one line

suppose we  have below if else statement

if x > 100:
   y = 1
   y = 0
so how to convert above if else statement using python if else one in line
y= 1 if x > 100 else 0

This is simple way to write if else one in line using python

See more

x if cond else y
in simple if condiotion is true the value is x otherwise y

see example

y= "prime" if x =7 else "Non prime"


NameError name slugify is not defined Django error

Most of the case When you tried to add a slug keyword for page model you will get error NameError: name ‘slugify’ is not defined Django error.Here we are discussing how to solve “NameError: name ‘slugify’ is not defined Django error”

Here is the model

from django.db import models

# Create your models here.
class page(models.Model):
 """Model representing a book (but not a specific copy of a book)."""
 page_id = models.AutoField(primary_key=True)
 page_title = models.CharField(max_length=200)

 author_name = models.CharField(max_length=100)
 page_content = models.TextField(max_length=1000, help_text='Enter your page content')
 page_name = models.SlugField(editable=False) # hide from admin

 def save(self):
 if not self.page_id:
 self.page_name = slugify(self.page_title)

 super(page, self).save()

Here is the error

“NameError: name ‘slugify’ is not defined Django error”


NameError name slugify is not defined Django error

How to Solve it

Step install  python-slugify module

# pip install python-slugify

Include slugify module in your model
 from slugify import slugify

NameError name slugify is not defined Django error 2


assertion error python

Assertions is one of the most advanced Exception Handling technique used in python by using the Assert statement,we can create debug message,if anything logically end up with an error.If anything fails logically AssertionError will display and end the process. here we will check assertion error python
When we are working with automatic car project their is a situation of fuel tank is empty that time we have to stop all process and stop the vehicle with an error message “no fuels”

See the syntax here

assert Expression[, Arguments]

Create a file with below function

def fuelLevel(fuel):
   assert (fuel > 0),"Sorry no fuel in the system"


Run from command prompt

assertion error python