Python was developed by Guido van Rossum in the early 1990s and its latest version is 3.10.4, we can simply call it as Python3. Python 3.0 was released in 2008. and is interpreted language i.e it’s not compiled and the interpreter will check the code line by line. This article can used to learn very basics of Python programming language.
So before moving on further.. let’s do the most popular ‘HelloWorld’ tradition and hence compare Python’s Syntax with C, C++, Java ( I have taken these 3 because they are most famous and mostly used languages).
# Python code for "Hello World"
# nothing else to type...see how simple is the syntax.
print("Hello World")
Note: Please note that Python for its scope doesn’t depend on the braces ( { } ), instead it uses indentation for its scope.
Now moving on further Lets start our basics of Python . I will be covering the basics in some small sections. Just go through them and trust me you’ll learn the basics of Python very easily.
If you are on Linux/Unix-like just open the terminal and on 99% linux OS Python comes preinstalled with the OS.Just type ‘python3’ in terminal and you are ready to go.
It will look like this :
The ” >>> ” represents the python shell and its ready to take python commands and code.
# Python program to declare variables
myNumber = 3
print(myNumber)
myNumber2 = 4.5
print(myNumber2)
myNumber ="helloworld"
print(myNumber)
Output:
3
4.5
helloworld
See, how simple is it, just create a variable and assign it any value you want and then use the print function to print it. Python have 4 types of built in Data Structures namely List, Dictionary, Tuple and Set.
List is the most basic Data Structure in python. List is a mutable data structure i.e items can be added to list later after the list creation. It’s like you are going to shop at the local market and made a list of some items and later on you can add more and more items to the list.
append() function is used to add data to the list.
# Python program to illustrate a list
# creates a empty list
nums = []
# appending data in list
nums.append(21)
nums.append(40.5)
nums.append("String")
print(nums)
Output:
[21, 40.5, String]
Comments:
# is used for single line comment in Python
""" this is a comment """ is used for multi line comments
# Python program to illustrate
# getting input from user
name = input("Enter your name: ")
# user entered the name 'harssh'
print("hello", name)
Output:
hello harssh
# Python3 program to get input from user
# accepting integer from the user
# the return type of input() function is string ,
# so we need to convert the input to integer
num1 = int(input("Enter num1: "))
num2 = int(input("Enter num2: "))
num3 = num1 * num2
print("Product is: ", num3)
Output:
Enter num1: 8 Enter num2: 6 ('Product is: ', 48)
# Python program to illustrate
# selection statement
num1 = 34
if(num1>12):
print("Num1 is good")
elif(num1>35):
print("Num2 is not gooooo....")
else:
print("Num2 is great")
Output:
Num1 is good
Syntax:
def function-name(arguments):
#function body
# Python program to illustrate
# functions
def hello():
print("hello")
print("hello again")
hello()
# calling function
hello()
Output:
hello
hello again
hello
hello again
Now as we know any program starts from a ‘main’ function…lets create a main function like in many other programming languages.
# Python program to illustrate
# function with main
def getInteger():
result = int(input("Enter integer: "))
return result
def Main():
print("Started")
# calling the getInteger function and
# storing its returned value in the output variable
output = getInteger()
print(output)
# now we are required to tell Python
# for 'Main' function existence
if __name__=="__main__":
Main()
Output:
Started
Enter integer: 5
![]() |
Download the notes
Python 3 basics
|
Download as PDF |
# Python program to illustrate
# a simple for loop
for step in range(5):
print(step)
Output:
0
1
2
3
4
# Python program to illustrate
# math module
import math
def Main():
num = -85
# fabs is used to get the absolute
# value of a decimal
num = math.fabs(num)
print(num)
if __name__=="__main__":
Main()
Output:
85.0