First Blog

The learning is an enjoyment. Moreover if you enjoy more you will learn more. And if you learn more you enjoy more To day I am writing about ternary operator in python. How it writes a code in a single line instead of writing 4- 5 lines of code. ✔ Consider a if statement : ##CODE## condition = True if condition: ....x = 1 else: ....x=0 print(x) ##endcode## Above code can be rewritten in python as : x=1 if condition else 0 print(x) ✌ Some times it is difficult to read large numbers we can use '_' without effecting the result num1 = 10000000000 num2 = 100000000 total = num1 + num2 print(total) num1 = 10_000_000_000 num2 = 10_000_000 total = num1 + num2 print(total) Both the code will give same result irrespective of placing an '_' in between If we want result to be formatted with '_' separator we can do so as: print(f'{total: ,}') *************************************** ::actvating venv in windows:: python3 -m venv venv venv\Scripts\activate.bat :::Building todo app indjango::: pip install django django-admin startproject todo cd todo python manage.py runserver ctr c python manage.py migrate python manage.py createsuperuser username email password password python manage.py runserver # Open website at local server 127.0.0.1:8000/admin # Enter username and password and we can ligin as user created # ctrl C to go to cmd prompt python manage.py startapp tasks # in sttings.py file add 'tasks' in installed_app # view.py from def index(requests): return HttpResponse('Hello World') create urls.py file in tasks tasks/urls.py from django.urls import path from . import views urlpatterns = [ path('', views.index) ] #in todo/urls.py file todo/urls.py from django.urls import path, include urlpatterns = [ .......... path('', include('tasks.urls') ] # In cmd prompt python manage.py runserver # local host at 8000 will serve views.py file # create folder templates in tasks folder and then create a tasks folder in templates ie >> tasks/templates/tasks make a file list.html in tasks folder templates/tasks/list.html <h3>To Do</h3> # models.py from django.db import models class = Task(models.Model): ........title = models.CharField(max_length = 200) ........complete = models.BooleanField(default = False) ........created = models.DateTimeField(auto_now_add = True) ........def __str__(self): ..............return self.title # Make migration and migrate #in commd prompt python manage.py makemigrations python manage.py migrate # Register in admin so that we access Task from admin admin.py from .models import * admin.site.register(Task) ##Goto admin panel add tasks #views.py from . models import * def index(request): ....tasks = Task.objects.all() ....context = {'tasks' : 'tasks'} ....return render(request, 'task/list.html', context) #task/list.html <h3>To Do</h3> {% for task in tasks %} ....<div> ......<p>{{ task }}</p> ....<div> {% endfor %}

Our Sidebar

You can put any information here you'd like.

  • Latest Posts
  • Announcements
  • Calendars
  • etc