Thursday, January 12, 2023

Python Mathematics In Hindi - (Python Mathematics क्या है ?)

www.codeinhindi.online

Python Mathematics

Math Constants In Python

नमस्कार दोस्तो आज इस आर्टिकल में जानेंगे कि mathematics python  क्या हैं ? और यह कितने प्रकार के होते हैं ? Python में numbers पर mathematical operations करने के लिए math functions का इस्तेमाल किया जाता है | Python के program में अगर math functions का इस्तेमाल करना हो तो 'math' module का इस्तेमाल किया जाता है |

Math Constants in Python


Math Constant Description
e Math की 'E' Property Euler का number return करता है |
inf infinity को return करता है |
nan not a number(nan) का वर्णन करता है |
pi pi की value को return करता है |
tau tau(τ) की value को return करता है |

Python datetime module in hindi - (Python में datetime module क्या है ?)

codeinhindi

datetime module in python

Date and Time Module Introduction :

नमस्कार दोस्तो आज इस आर्टिकल में जानेंगे कि Python में datetime module क्या हैं ? और यह कितने प्रकार के होते हैं ? हर एक Programming Language में Date और Time का concept होता है | Python में भी Date और Time का concept होता है | Date और time के लिए कुछ modules को import करना पड़ता है | वैसे ही कुछ Modules नीचे दिए गए है | 

time Module 

datetime Module 

calendar Module 

time

time module पे बहुत सारे date और time से related functions, classes और constants होते है | 

datetime :

 datetime module में भी constants, classes; date और time से related होते है | ये module date और time के साथ काम करने के लिए Object-Oriented Programming में बनाया गया है | 

calendar : 

Calendar module में calendar से related कुछ functios और classes होते है |

Ticks/Timestamp in Python

12.00am January 1, 1970 से अबतक के बीते हुए seconds को 'Ticks' कहते है | 

Python में 12.00am January 1, 1970 से अबतक के बीते हुए seconds को return करने के लिए time module में 'time()' ये function होता है | ये time() function बीते हुए seconds को floating-point number में return करता है |

For Example,

import time
print("Timestamp/ticks :",time.time())

#Output : 
#Timestamp : 1497628217.2675047

What is epoch in Python? 

epoch ये हमेशा 12.00am January 1, 1970 होता है | Python में time module के 'gmtime()' इस function से epoch को return किया जाता है |

import time
print("epoch :",time.gmtime(0))
Output :

epoch : 
time.struct_time(
tm_year=1970, 
tm_mon=1, 
tm_mday=1, 
tm_hour=0, 
tm_min=0, 
tm_sec=0, 
tm_wday=3, 
tm_yday=1, 
tm_isdst=0)

Sunday, January 8, 2023

Function In Python (Python Function क्या है ?)

Python

Python - What is Function 

function in python

Python Function ये statements का एक समूह होता है | हर एक Program में एक function तो होता ही है| जहाँ पर Function की statements की जरुरत होती है वहाँ पर Function को call किया जाता है | 

Python Functions Advantages 

Function में लिखा हुआ code बार-बार लिखना नहीं पड़ता | 

बड़े Program को छोटे-छोटे function में विभाजित किया जा सकता है |

Function Programmer का समय, Program की space और memory बचाता है | 

अगर Program में कहा पर error आ जाए तो उसे आसानी से निकाला जा सकता है | 

जहाँ पर जरुरत हो वहाँ पर function को बार-बार call किया जा सकता है | 

Types of Function - Python main function

In-Built/Predefined Function 

User-Defined Function 

1. In-Built/Predefined Function 

Python में अभीतक print() नामक function बहुत ही बार इस्तेमाल किया है | print() function ये Python में in-built function है | 

2. User-Defined Function 

Python में user-defined function में दो प्रकार पड़ते है | Function Definition Function Call 

2.1 Function Definition 

Syntax for Function Definition

def function_name(parameter(s)):
	"function_docstring"
	function_body
	return statement

def : Python में function को create करना हो तो शुरुआत में 'def' keyword का इस्तेमाल किया जाता है | 

function_name : def keyword के बाद function का नाम दिया जाता है | हर function का नाम उसके statement से related हो तो अच्छा रहता है | 

(parameter(s)) : Optional. parameters optional होते है parenthesis(()) नहीं होते है | function के name के बाद parenthesis(()) दिया जाता है | उन parenthesis में एक या एक से ज्यादा parameters दिए जाते है | parameters; optional होते है | 

: : parenthesis के बाद colon(:) देना अनिवार्य होता है | colon देने के बाद Python interpreter द्वारा automatic code indent होता है | 

"function_docstring" : Optional. यहाँ पर documentation string दिया जाता है | 

function_body : Optional. ये function की body होती है | जिसमे कुछ local variables और statements हो सकते है | body में दिए हुए variables को global भी बनाया जा सकता है | 

return statement : Optional. ये return statement होता है | return statement के लिए 'return' keyword का इस्तेमाल किया जाता है | ये statement से function end होता है | अगर return statement दिया नहीं जाता है तो default 'None' return होता है | 

Example for Function Definition 

Source Code :

#Function definition
def func(param):          #function name, parameter and colon        
    "This is a docstring" #docstring
    print(func.__doc__)   #function body
    return param          #return statement

Example पर 'func' ये function का नाम है |

उसके बाद parenthesis(()) में 'param' नाम का एक parameter और parenthesis के बाहर colon(:) दिया गया है |

function के indent code में पहले docstring दी गयी है | 

उसके बाद function की body में class attribute की मदद से docstring को print किया गया है और आखिर में return statement में 'param' इस parameter को print किया गया है | 

2.1 Function Call 

Function के call में सिर्फ function का नाम और अगर function को parameter हो तो parameter की value दी जाती है | जबतक function को call नहीं किया जाता तबतक function का code execute नहीं होता है | 

Example for Function Call 

Source Code :

#Function definition
def func(param):          #function name, parameter and colon        
    "This is a docstring" #docstring
    print(func.__doc__)   #function body
    return param          #return statement

print("Call 1")
print(func(5))            #Function call

print("Call 2")
print(func(10))           #Function call
Output :

Call 1
This is a docstring
5
Call 2
This is a docstring
10

Python में जब function को call करके जिस data type की value as a argument दी जाती है तब उसका data type decide हो जाता है | 

Function Argument in Python 

Python में चार प्रकार के function parameter होते है | 

Default Argument 

Required Argument

Keyword Argument 

Variable Number of Argument 


1. Default Argument 

Default argument में definition पर argument के लिए default value '='(assignment operator) से set की जाती है | 

Function Call पर जब definition पर assign किया हुआ argument नहीं दिया जाता तो default value pass की जाती है | 


Source Code :

def DefArg(num=5, str="Hello"):
    print(num)
    print(str)

print("Call1")
DefArg()
print("Call2")
DefArg(8)
print("Call3")
DefArg(8, "Hii")
Output :

Call1
5
Hello
Call2
8
Hello
Call3
8
Hii

2. Required Argument 

Required Argument में function call पर argument देना अनिवार्य होता है | 

जब function definition पर argument दिया जाता है तब उसे function call पर उसकी value देना required होता है | 


Source Code :

def ReqArg(num, str):
    print(num)
    print(str)

print("Call1")
ReqArg(5, "Hello World")

print("Call2")
ReqArg("Hello World", 5)
Output :

Call1
5
Hello World
Call2
Hello World
5

अगर function call पर argument दिया नहीं जाता है तो , TypeError का exception आ जाता है | 

अगर function पर दो arguments है | अगर call करते वक्त एक ही argument value दी जाती है तो तब भी 'TypeError' exception आ जाता है | 

Python Control Statements (what is control statements (structures) in hindi?)

Python

Python Control Statements

Programming भाषा में, प्रोग्राम के execution के flow को नियंत्रित करने के लिए जो स्टेटमेंट्स या structures प्रयोग किये जाते है उन्हें control statements(structures) कहते है|

Python - If Statement 

Python में if, if_else और if_elif_else ये तीन प्रकार के Control Statements होते है |

इन control statements से जब तक दिया हुआ expression true नहीं होता तब तक ये अपना statement; execute नहीं होता है | 

if Statement में जब expression true होता है तब वो अपने statement को execute होता है और अगर expression false होता है तब वो execute नहीं होता है | 

Syntax for if Statement :

if expression :
	if_statement(s)
Example for if Statement Source Code :

a = 2
b = 3

if(a < b) :
    print("a is less than b")
Output :

a is less than b

Python - If Else Statement 

Python में if, if_else और if_elif_else ये तीन प्रकार के Control Statements होते है | 

इन control statements से जब तक दिया हुआ expression true नहीं होता तब तक ये अपना statement; execute नहीं होता है | 

if Statement में जब expression true होता है तब वो अपने statement को execute होता है और अगर expression false होता है तब else का statement; execute होता है | 


Syntax for if_else Statement :

if expression :
	if_statement(s)
else :
	else_statement(s)
Syntax for if_else Statement Source Code :

a = 2
b = 2

if(a < b) :
    print("a is less than b")
else :
    print("a is greater than or equal to b")
Output :

a is greater than or equal to b

Python - If Elif Else Statement 

Python में if, if_else और if_elif_else ये तीन प्रकार के Control Statements होते है |

इन control statements से जब तक दिया हुआ expression true नहीं होता तब तक ये अपना statement; execute नहीं होता है |

if Statement में जब expression true होता है तब वो अपने statement को execute होता है और अगर expression false होता है तब flow elif पर जाता है अगर elif का expression; true होता है तो उसका statement execute होता है और elif का expression; false होता है तब else का statement execute होता है | 

Syntax for if_else Statement :

if expression :
	if_statement(s)
elif expression :
	elif_statement(s)
else :
	else_statement(s)
Syntax for if_else Statement Source Code :

a = 2
b = 2

if(a < b) :
    print("a is less than b")
elif(a > b) :
    print("a is greater than b")
else :
    print("a is equal to b")
Output :

a is equal to b

Python - Pass Statement 

pass Statement ये null Statement होता है | जब pass Statement execute होता है तब कुछ नहीं होता है | 

Program में जब functions, loops, classes या control statement लिखा जाता है उनकी body उसके निचे ही लिखी जाती है लेकिन अगर उनकी body बाद में लिखनी हो तो 'pass' Statement का इस्तेमाल किया जाता है | 

Syntax for pass Statement :

pass
Example for pass Statement Source Code :

class A: pass

def func(param):
    pass

func(10)
print("Hello World")
Output :

Hello World

Python - Break Statement 

Python में break Statement Loop को किसी expression पर बंद कर देता है | 

Syntax for break Statement :

break

Example for break Statement Example में अगर 'n' को 4 मिल जाता है तब for Loop का iteration बंद हो जाता है | 

Source Code :

nums = [1, 2, 3, 4, 5]
for n in nums :
   print(n)
   if(n == 4):
      break  
Output :

1
2
3
4

Python - Continue Statement 

continue Statement से Loop में iterate हो रहे कुछ statement(s) को skip करके अगले statements को execute करता है | 


Syntax for continue Statement :

continue
Example for continue Statement Source Code :

nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for n in nums :
   if(n == 5) :
      print("Skipped Element :", n)
      continue
   print(n) 
Output :

1
2
3
4
Skipped Element: 5
6
7
8
9
10


अगर आपको यह पोस्ट 📑 (Python Control Statements) पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !

Tuesday, January 3, 2023

Loops In Python In Hindi (Python में loops क्या हैं ? )

Python Loops

Introduction Python Loops

सभी प्रोग्रामिंग लैंग्वेज ( जैसे की C++ , java आदि ) में हमें loops की सुविधा प्रदान की जाती है उसी प्रकार पाइथन लैंग्वेज मे भी loops का उपयोग किया जाता है | Loops का उपयोग कोड को बार बार execute करने के लिए होता है| 

जैसे की हमे किसी कोड या फंक्शन को 200 से ज्यादा बार execute करना है तो हमे इस कोड को उतनी ही बार कॉल करना पड़ेगा या लिखना पड़ेगा जितनी बार कोड को execute करना है इस प्रोसेस मे हमे कोड को लिख़ने मे ज्यादा समय लग जाता है | 

इस प्रॉब्लम को दूर करने के लिए हम लूप्स का प्रयोग करते है | लूप्स के द्वारा हम जितनी बार चाहे उतनी बार प्रोग्राम को execute करा सकते है | इससे हमे कोड को बार बार कॉल करने की जरूरत नहीं पड़ती | 

 loops मे हम condition का उपयोग करते है जिससे की हम लूप्स को start और end करा सकते है | लूप्स मे अगर हमारी कंडीशन true होती है तो code / loops execute होता रहेगा जैसे की कंडीशन false होती है कोड terminate हो जाता है

types of loops in python

पाइथन मे तीन प्रकार के लूप्स होते हैं |

  1. while loop
  2. for loop
  3. nested

loop in python

while लूप को हम आसानी से define कर सकते है | ये एक simple लूप है | While loop, दी गई शर्त जब तक TRUE है, तब तक यह स्टेटमेंट्स (कोड) को लगातार execute करता है। यह पहले condition को check करता है और फिर instruction देता है|

Syntax for while loop in python

while (expression):
	while_statement(s)

उदहारण :- यदि हमें hello word को 10 बार प्रिंट कराना है तो इसे दो तरीके से सकते हैं | 1. पहला या तो हम 10 बार hello word को लिख कर प्रिंट कराये | जिससे की प्रोग्राम काफी बड़ा हो जायेगा | २. दूसरा हम loop का प्रयोग करके hello word को print करे | जिससे प्रोग्राम काफी सरल हो जायेगा | हम अब देख़ते है की लूप द्वारा हम कैसे hello word को 10 बार print करेंगे |

i= 1
while i<=10:
    print ("hello word")
    i=i+1

Step 1:- सबसे पहले हमने एक variable i= 1 initialize किया जिससे की हमें यह समझने मे आसानी होगी की while loop कैसे काम करेगा | step 2:- फिर हम while कीवर्ड के साथ condition (while i<=10:) लिख़ते है | अगर हमे लूप 1० बार चलाना हो हम (while i<=10:) लिखेंगे और अगर 100 बार चलना हो तो (while i<=100:) लिखेंगे | step 3:- फिर हम statement लिखेंगे जो हमे प्रिंट करवानी है | step 4:- फिर i की value को increment करेंगे जिससे की i की value चेंज होती रहेगी | जब तक कंडीशन i<=10 false नहीं होती तब तक लूप चलता रहेगा और हेलो वर्ड प्रिंट होता रहेगा | for loop in python पाइथन मे for loop का syntax बाकि ओर प्रोग्रामिंग लैंग्वेज से काफी अलग होता है | for loop का प्रयोग किसी sequence ( जैसे की list, tuples, dictionaries और set आदिजैसे की list, tuples, dictionaries और set आदि ) को iterate करने के लिए किया जाता है |

for < Variable_Name > in < Sequence_Name >
    statements

उदहारण :- ऊपर दिए गए उदहारण को हम for loop के सहायता से 10 बार प्रिंट कराएँगे |

for i in range (10):
    print ("hello word !")

Step 1:- सबसे पहले हम for कीवर्ड को लिख़ते है | step 2:- फिर वेरिएबल का नाम लिख़ते है जो की sequence को iterate करेगा | step 3:- फिर in ऑपरेटर को लिख़ते है | step 4:- उसके बाद हम sequence का नाम लिखेंगे जिसको iterate करना है | step 5:- फिर हम statement लिखेंगे जो हमें प्रिंट करनी है | break and continue keyword break keyword का प्रयोग हम लूप को ख़त्म करने लिए करते हैं |

for i in range (1 , 11):
    if i==5:
        break 
    print (i)

Output :

1
2
3
4

ऊपर दिए गए example मे जब i == 5 होगा तो लूप ब्रेक हो जायेगा ओर हमे ऊपर दिया गया आउटपुट मिलेगा | continue keyword , loopके current iteration को stop करता है ओर फिर continue आगे बढ़ता है |

for i in range (1 , 11):
    if i==5:
        continue
    print (i)

Output:

1 2 3 4 6 7 8 9 10

ऊपर दिए गए example मे जब i == 5 होगा तो current iteration stop हो जायेगा ओर हमे ऊपर दिया गया आउटपुट मिलेगा | nested loop in python पाइथन मे भी हम बाकि प्रोग्रामिंग लैंग्वेज की तरह एक लूप के अंदर दूसरा लूप define कर सकते हैं |

for i in range(1, 4):
    for j in range(i):
         print(i, end=' ')
    print() 

Output :

1 
2 2 
3 3 3 


अगर आपको यह पोस्ट 📑 (Loops In Python In Hindi)पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !

Thursday, December 29, 2022

Python Input Output In Hindi (Python Input Output क्या है ?)

Python Full Course in Hindi

input output in python

Input and Output Introduction 

Python में input/output के लिए कुछ functions का इस्तेमाल किया जाता है | User के keyboard से input लेने के लिए 'input()' इस function का इस्तेमाल किया जाता है और User के screen पर कुछ print करने के लिए 'print()' इस function का इस्तेमाल किया जाता है |

input() Function for taking User Input Python में जब User से कुछ letters या numbers लेने की बारी आती है तब 'input()' function का इस्तेमाल किया जाता है |

Syntax for input() Function

input("SomeText")

Parameter : 

Some Text : Optional.User को क्या input देना है उसके बारे में यहाँ पर कुछ text दिया जाता है | Example for 'input()' 

Function Source Code :

a = input("Enter your name : ")
print("Your name is", a)
Output :

Enter your name : Maxon
Your name is Maxon

जब User; numeric, letters या special symbols; input लेते है तो उसका data type 'string' होता है |

Source Code :

a = input("Enter Number : ")
print("Entered input's type is", type(a))
Output :

Enter Number : 4
Entered input's type is 

Python Type Conversion 

जब उस String data type को Number(int, float, complex number) में convert करना हो तो Type Conversion functions का इस्तेमाल करना पड़ता है | 

 Source Code :

a = input("Enter Number : ")
print("Entered input's type is", a)

b = int(a)
print("Entered input's type is", b)
print("Entered input's type is", type(b))

c = float(a)
print("Entered input's type is", c)
print("Entered input's type is", type(c))

d = complex(a)
print("Entered input's type is", d)
print("Entered input's type is", type(d))
Output :

Enter Number : 4
Entered input's type is 4
Entered input's type is 4
Entered input's type is 
Entered input's type is 4.0
Entered input's type is 
Entered input's type is (4+0j)
Entered input's type is 

Python Print Function print() 

Function for Displaying Output on Screen 

अभी तक User के screen पर output दिखाने के लिए 'print()' function का इस्तेमाल किया गया था, लेकिन print() function के लिए एक से ज्यादा parameters होते है | जिनका इस्तेमाल एक से ज्यादा newline देना या file handling के लिए किया जाता है | 

Regular Example for print() Function

a = 5
print("Value of a is", a) #Output : Value of a is 5
Syntax for print() Function

print(value1,value2,...,valueN, sep="", end="\n", file=sys.stdout, flush=False)

Parameter : 

value1,value2,...,valueN, : 

यहाँ पर एक या एक से ज्यादा values दी जाती है | हर value को comma(,) से seperate किया जाता है | 

 sep=" " : 

Optional. जब एक से ज्यादा values दी जाती है तो default 'sep' parameter द्वारा space( ) दिया जाता है | User चाहे तो 'sep' parameter पर कोई भी value दे सकता है | 

 end="\n" :

 Optional. 'end' parameter पर '\n'(newline) default दिया जाता है | User चाहे तो 'end' parameter पर कोई भी value दे सकता है |

 file=sys.stdout :

 Optional. 'file' parameter पर 'sys.stdout'(print entered text on screen) default दिया जाता है | User चाहे तो 'file' parameter पर किसी भी file को open कर सकता है |

 flush=False : 

Optional. 'flush' parameter पर 'False' ये value default होती है | जब False दिया जाता है तब stream को flush नहीं किया जाता है या जब 'True' दिया जाता है तब stream को जबरन flush किया जाता है | 

 print() function null/None return करता है | 

 Example for print() Function with 'sep' Parameter Example पर '!!!!!' इस string से separate किया गया है |

Source Code :

print("Hello World", "Hello Friends", "Hello Maxon", sep="!!!!!")
Output :

Hello World!!!!!Hello Friends!!!!!Hello Maxon

Example for print() Function with 'end' Parameter 

Example में end पर '\n\n\n\n\n' इस string दिया गया है | 

 Source Code :

print("Hello World", end="\n\n\n\n\n")
print("Hello Friends")

Output :

Hello World




Hello Friends

Example for print() Function with 'file' Parameter 

Example पर print() function के 'file' parameter द्वारा 'textfile.txt' इस file पर 'Hello World' ये string write किया गया है |

Source Code :

f = open("textfile.txt", "w")
print("Hello World", file = f)
f.close()
textfile.txt

Hello World

Note : print() function में binary files पर operation नहीं किया जाता है | इसके बदले में write() function का इस्तेमाल किया जाता है | 


 अगर आपको यह पोस्ट 📑 (Python Input Output In Hindi)पसंद आई हो तो अपने मित्रों के साथ जरूर शेयर करें। धन्यवाद !