how to define a function in python

As per the latest trends set python is the most exploring programming language,expert says in the middle of 21st century 70% of the day life application will be depend on python programming language, python and artificial intelligence definitely will brings miracle in the middle of 21st century.So being a programmer without python programming language you cant survive in 21st century.Like other programming language function in python is also so important.so basically our question is how to define a function in python.To define a function in python programming language, python has its on syntax. lets see the syntax

def FUNCTION-NAME( PARAMETER):
    FUNCTION_BLOCK
   return [EXPRESSION]

def            => Function blocks begin with this keyword def 
FUNCTION-NAME  => Name of the function you can define your own name

PARAMETER      => It the Parameter passing to the function when function was called

FUNCTION_BLOCK => Function code block

EXPRESSION     =>  return [EXPRESSION] passing back an expression to the caller

See example

def sum(a,b):
 resultvalue = a+b
 return resultvalue

x =10
y = 40
print(sum(x,y));
x =60
y = 40
print(sum(x,y));

how to define a function in python

def printPrime(lower,upper):
 print( "prime numbers between "+str(lower)+ ", "+str(upper))
 for num in range(lower,upper + 1):
    if num > 1:
       for i in range(2,num):
           if (num % i) == 0:
               break
       else:
           print(num)

x =10
y = 20
printPrime(x,y);
x =1180
y = 1200
printPrime(x,y);

how to define a prime number display function in python

python print function syntax

Most of the beginners in python always confuse with print function and start searching for print function in google.Like other programming language python print out put function also easy. In this tutorial we are going to explore python print ,the way to print the out put in python. print() is the function used in python to print out put.

Let see example

python print syntax

print(object(s), separator=separator, end=end, file=file, flush=flush)

object = any object, it will convert in to string and print

sep=’separator’ = Optional default ‘ ‘

end=’end’ = Default is line feed, will tell what print at the end

file = Default is sys.stdout , object with a write method.

1) python print a string “Hello World”


print("Hello World")
Output:

Hello World

2) python print a variable numberstart =10

 
numberstart =10
print(numberstart)
Output:

10


3) python print : managing tuble


tupleA = (2, 4, 8, 12) 
tupleB = ('red', 'blue')  
print(tupleA + tupleB ) 

Output:

(2, 4, 8, 12, 'red', 'blue')

python print function

python how to check if file exists or not / directory exists or not

we know than python is the most exposing programming language in the artificial intelligence world ,so many of the using python as the primary programming language for building their applications,in some application we may come across a situation to check whether the file exists or directory exists using python. so in this section let see in python how to check if file exists or not / directory exists or not, fortunately we have python OS library function to check if file exists or not / directory exists or not. os.path.isdir is used in python to check if directory exists and os.path.exists() is used in python to check if file exists or not.

Lets See the Syntax
1) os.path.isdir() it return True or False based on if directory exists or not

#pythontest.py

import os 
print(os.path.isdir("D:/testvs"))    // Already one directory exists
print(os.path.isdir("D:/testvs2020"))  // directory not exists

OUT PUT

True
False



In python how to check if file exists or directory exists

2) os.path.exists() it return True or False based on if file exists or not

#pythontest.py

import os
print(os.path.exists("D:/testvs2020/pythontest.py"))
print(os.path.exists("D:/testvs/pythontest.py"))

OUT PUT

False
True


In python how to check if file exists or not

How to create a small project using Django

In the last tutorial we learned how to install a Django so in this section we are going to discuss how to create a small project using Django. we already created Django admin users using the Django manage.py createsuperuser .In this “create a small project using Django” tutorial we are going to create a small blog that allow users to post their thoughts one by one lets see in detail

Steps to create a small project using Django

1) Create your app first

#python manage.py startapp tutorialshore

How to create a small project using Django step 1

See the automatically created folder structure

How to create a small project using Django step 1 project structure

__init__.py : package files
admin.py : This files is help to make the changes in the admin interface
models.py : This is one of the important files all the application are stored in this files
tests.py : Create your tests here.
views.py : This is another important files all the views interface managed by this files

 

2) We have to change URL pattern open myblog =>myblog=>urls.py

Add below code

from django.urls import include
from django.urls import path
urlpatterns += [
    path('tutorialshore/', include('tutorialshore.urls')),
]

Then urls.py become

"""myblog URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
	
]
from django.urls import include
from django.urls import path
urlpatterns += [
    path('tutorialshore/', include('tutorialshore.urls')),
]

3) Create a new urls.py file inside tutorialshore folder and add below lines

 

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('post/', views.post, name='post'),
  
]

4)  Open view.py files add below code

 

from django.shortcuts import render
from django.http import HttpResponse

def post(request):
 text = """<h1>welcome to my post View !</h1>"""
 return HttpResponse(text)
 
def index(request):
 text = """<h1>welcome to my blog!</h1>"""
 return HttpResponse(text)

# Create your views here.



5) Run # python manage.py runserver

5) Open http://127.0.0.1:8000/tutorialshore/ in your browser

 

How to create a small project using Django step 2

6) open http://127.0.0.1:8000/tutorialshore/post/

How to create a small project using Django step 3

Next => How to create new admin user or super user in Django python