Python Language Overview

I am Developer, Artist and trying my luck on blogging as well. Well I am Ambitious, Passionate towards Learning, The Night Owl, And I Like challenges...
Search for a command to run...

I am Developer, Artist and trying my luck on blogging as well. Well I am Ambitious, Passionate towards Learning, The Night Owl, And I Like challenges...
No comments yet. Be the first to comment.
In this series you will get the fundamentals of python programming, and it will help you write a clean and effective scripts in future.
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, data types are actually classes ...
Post #2 in the Complete Prompt Engineering Series Welcome back! In What is Prompt Engineering? A Complete Introduction, you learned what prompt engineering is and why it matters. Now we're going deeper: understanding the engine under the hood. You do...

Welcome to the future of human-AI collaboration. If you're reading this in 2024-2025, you're witnessing a fundamental shift in how humans interact with machines—and prompt engineering is your passport to this new world. The Definition: What Exactly I...
In this post, we’ll walk through the anatomy of a great prompt, illustrate every step with vivid examples, and even peek under the hood to see what happens technically when you hit “send.” By the end, you’ll be able to craft prompts that unlock the f...

What is GenAI and Why Does Prompting Matter? If you’ve ever wondered how people interact with AI tools, or why some folks seem to get exactly what they want from tools like ChatGPT while others receive confusing or generic responses, this article is ...

Let’s be honest—being a developer isn’t just about writing code. It’s about solving problems, dealing with burnout, handling meetings, chasing deadlines, and finding time to learn, debug, and ship. But in the middle of this chaos, one simple habit ca...

This topic covers Bird's-eye view of the language as a whole. We'll use examples to describe different elements of Python's syntax without getting into much detail about how each feature works. The idea is for you to become familiar with the syntax of the language so that you may better learn its features later.
Few keypoints:
The Hello World program is a traditional first step in learning a new language, but it's actually more than that. The purpose of Hello World is to validate your development environment. Whenever you set up a new development environment, it's a good idea to use a simple, minimal, and functional program to confirm that your development cycle functions as you expect, and that's what Hello World is for.
print("Hello World")
Hello World
print("Hi, I am learning Python!!")
Hi, I am learning Python!!
from datetime import datetime
def print_date():
date=datetime.today()
print(date)
if __name__=="__main__":
print_date()
2021-09-19 16:06:22.352907
# expressions
x=5
y=10
def z(x,y):
return y/x
print(x+y) # addition operation, hence it will return a value
print(x*y) # multiplication operation, hence it will return a value
print(True) # built-in constant value
print((x,y)) # tuple
print(z(x,y)) # function call
15
50
True
(5, 10)
2
print("this is statement one!"); print("this is statement two!!")
this is statement one!
this is statement two!!
Unlike most other modern scripting languages, whitespace is significant in Python. There's no brackets or parentheses to match up just indents.
for example in C block or scope of a function is determined using braces:
function main()
{
printf("the is an example in c language");
}
whereas, In python there is no brackets or braces to determine. Indentation determine the block and scope of a function
# example 1
import platform
def main(): # Block started for function main()
message() # statement inside function main() block
def message(): # Block started for function message()
print('This is python version {}'.format(platform.python_version())) # statement inside function message() block
if __name__ == '__main__': main()
This is python version 3.7.4
# example 2
import platform
def main(): # Block started for function main()
message() # statement inside function main() block
def message(): # Block started for function message()
print('This is python version {}'.format(platform.python_version())) # statement inside function message() block
print("statement 2")
print("statement 3")
if __name__ == '__main__': main()
This is python version 3.7.4
statement 2
statement 3
# example 3
import platform
def main(): # Block started for function main()
message() # statement inside function main() block
def message(): # Block started for function message()
print('This is python version {}'.format(platform.python_version())) # statement inside function message() block
print("statement 2")
print("statement 3")
if __name__ == '__main__': main()
statement 3
This is python version 3.7.4
statement 2
# example 4
import platform
def main(): # Block started for function main()
message() # statement inside function main() block
def message(): # Block started for function message()
print('This is python version {}'.format(platform.python_version())) # statement inside function message() block
if True:
print("statement 2") # block of if
else:
print("statement 3") # block of else
if __name__ == '__main__': main()
This is python version 3.7.4
statement 2
In python, Comments are introduced by pound sign (#), there is nothing like mutli-line comments in python. But Docstring can be used as comments where you can define the functionality or give description about the block or function.
DocString : Triple double coat (""") and single coat (''')
# this is comment
def add(x,y):
z=x+y # addition of two variable x & y and assignment of the sum in z variable
print(z) # printing the result
add(5,10)
15
"""
Author: xyz
Description:
Writes the words Hello World on the screen
"""
def print_msg():
print("hello world!!")
sub(10,5)
def sub(x,y):
'''
Author: abc
Description:
Writes result of x-y on the screen
'''
print(x-y)
print_msg()
hello world!!
5
In earlier version of python, i.e python 2, Print was not a function and strings were not an object for ex:
print "hello world!!
age = 5
print "I am bob, and i am %d years old" % age
but in python 3, same code can be written as:
# example 1
print ("Hello World!!")
# example 2
age =5
print("I am Bob, I am {} years old".format(age))
# example 3
print(f"I am Bob, I am {age} years old")
#example 4
print("this is a string 1","this is string 2")
#example 5
x,y=5,10
print(x,y)
Hello World!!
I am Bob, I am 5 years old
I am Bob, I am 5 years old
this is a string 1 this is string 2
5 10
Python differs from many languages in how blocks are delimited. Python does not use brackets or any special characters for blocks, instead it uses indentation.
# block example
x = 42
y= 73
if x < y:
print('x < y: x is {} and y is {}'.format(x,y))
x < y: x is 42 and y is 73
# example 2
x =10
y = 5
if x < y:
print('x < y: x is {} and y is {}'.format(x,y))
print("something else!!")
something else!!
# example 2
x =10
y = 5
if x < y:
print('x < y: x is {} and y is {}'.format(x,y))
print("line 2")
print("line 3")
print("line 4")
print("line 5")
File "<tokenize>", line 10
print("line 5")
^
IndentationError: unindent does not match any outer indentation level
x =10
y = 5
if x > y:
print('x < y: x is {} and y is {}'.format(x,y))
print("line 2")
print("line 3")
print("line 4")
print("line 5")
x < y: x is 10 and y is 5
line 2
line 3
line 4
line 5
Note: Blocks do not define scope in Python. Functions, objects, and modules do define scope in Python. for ex:
# example block doesn't define the scope of a variable
x =10
y = 5
if x > y:
z= 100
print('x < y: x is {} and y is {}'.format(x,y))
print("z is {}".format(z))
x < y: x is 10 and y is 5
z is 100
# example function can define the scope of a variable
def sqr():
x=10**2
sqr()
print(x)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-4731039992ca> in <module>
7 sqr()
8
----> 9 print(x)
NameError: name 'x' is not defined
Conditional Statement in Python perform different computations or actions depending on whether a specific Boolean constraint evaluates to true or false. Conditional statements are handled by IF statements in Python.
# example if-else
x=7
if x%2==0: # condition if x is fully divisible by 2 i.e remainder = 0 then print it's EVEN else print it's ODD
print("X is even")
else:
print("X is odd")
X is odd
# example if-elif-else
x=10
y=20
if x<y:
print("X is less than Y")
elif x>y:
print("X is greater than Y")
else:
print("X & Y are equal")
X is less than Y
# nested if else
import random
day=random.randint(0, 6)
if day==0:
print("it's Monday")
elif day==1:
print("it's Tuesday")
elif day==2:
print("it's Wednesday")
elif day==3:
print("it's Thursday")
elif day==4:
print("it's Friday")
elif day==5:
print("it's Saturday")
elif day==6:
print("it's Sunday")
it's Wednesday
Python provides two basic types of loops.
A while loop tests a conditional expression and the body of the loop is executed while the condition remains true.
A for loop iterates over a sequence and the body of the loop is executed for each element of the sequence and until the sequence is exhausted.
# while loop
n=0
words =['one','two','three','four','five']
while(n<5): # execute utill n = 5
print(words[n], end=' ')
n+=1
one two three four five
# fibonacci series in while loop
# F 0 = 0, F 1 = 1, and then using the recursive formula F n = F n-1 + F n-2 to get the rest.
a,b=0,1
while b< 1000:
print(b , end=' ')
a,b=b,a+b
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
# for loop
words =['one','two','three','four','five']
for i in words:
print(i, end= " ")
one two three four five
Functions in Python serve the purpose of both functions and subroutines in other languages.
Python Functions is a block of related statements designed to perform a computational, logical, or evaluative task. The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
Functions can be both built-in or user-defined. It helps the program to be concise, non-repetitive, and organized.
syntax:
def function_name(parameters):
"""docstring"""
statement(s)
return expression
# example simple working of a function
def func(n):
print(n)
func(40)
40
# example with default parameter
def func(n=1):
print(n)
func()
1
# example function always return a value
def func(n=1):
print(n)
x= func()
print(x)
1
None
Class is a definition and object is an instance of a class. self represent reference to an object when class is used to create an object. we use the dot as the de-reference operator to reference members of the object.
class Duck: # class definition
sound="Quaack!" #class variable or property
walks="walks like a duck" #class variable or property
def quack(self): #class method
print(self.sound)
def walk(self): # class method
print(self.walks)
def main():
donald= Duck() # creating an object
donald.quack() # calling class method quack using object
donald.walk() # calling class method walk using object
if __name__ == "__main__": main()
Quaack!
walks like a duck