Introduction
Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability with its notable use of significant whitespace. Its language constructs and object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
Python is a multi-paradigm programming language. Object-oriented programming and structured programming are fully supported, and many of its features support functional programming and aspect-oriented programming (including by metaprogramming and metaobjects (magic methods)). Many other paradigms are supported via extensions, including design by contract and logic programming.
Python uses dynamic typing and a combination of reference counting and a cycle-detecting garbage collector for memory management. It also features dynamic name resolution (late binding), which binds method and variable names during program execution.
Python's design offers some support for functional programming in the Lisp tradition. It has filter, map, and reduce functions; list comprehensions, dictionaries, sets, and generator expressions. The standard library has two modules (itertools and functools) that implement functional tools borrowed from Haskell and Standard ML.
Rather than having all of its functionality built into its core, Python was designed to be highly extensible. This compact modularity has made it particularly popular as a means of adding programmable interfaces to existing applications. Van Rossum's vision of a small core language with a large standard library and easily extensible interpreter stemmed from his frustrations with ABC, which espoused the opposite approach.
Hello World
The first program for every programmer is the famous "Hello World". In Python, you can write the program in many ways but not limited to the following example code.
def main():
print('Hello, World!')
main()
# Hello, World!
Data Types
Python has the following data types:
- Integer
- Float
- Complex Numbers
- String
- List (Array)
- Tuple
Example:
# integer
number = 5
# float
fraction = 5.5
# complex number
complex = 7 + 26i
# string
name = 'Python'
# list (array)
numbers = [726, 727, 728, 729, 730]
# tuple
keys = (726, 'connect', 60.9)
Variables
Python is a dynamically typed language. Unlike the statically typed languages, the data type is not specified before the initialization of a variable.
Rules for creating variables
In Python, rules for creating variables are similar to other languages. They are as follows:
- A variable name must start with a letter or the underscore character.
- A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ).
- Variable names are case-sensitive (name, Name and NAME are three different variables).
- The reserved words(keywords) cannot be used naming the variable.
Variables can be initialized as follows:
# integer
var = 0
# empty string
username = ''
# float
average = 0.0
Multiple variable assignment
Python allows multiple assignment of variables simultaneously, i.e. on a single line of code.
# assign multiple value on single line
name, age, gender = 'Siri', 9, 'F'
One variable, different data types
Variable already in use with a data type, can be used to assign value of different data type as well. For Example:
# integer
var = 267
# string
var = 'Two Sixty Seven'
Conditional Statements
Conditional statements are one of the fundamental & important part of any programming language. These statements help us create a decision tree on which action to be performed on certain condition.
if .. else
Basic if .. else statement looks like the following code example:
driver_age = 17
# Driver is eligible only if age is equal to or above 18
if(driver_age >= 18):
print('You are eligible for driving')
else:
print('Sorry, you need to at least 18 in order to drive')
if .. else ladder
When there are multiple condition which are to be checked in the conditional statement, if else ladder is one way to achieve it.
x, y = 7, 7
if(x < y):
print('x is less than y')
elif(x > y):
print('x is greater than y')
else:
print('x is equal to y')
Loops
Looping saves a lot of time in programming by performing repeated tasks wrapped inside them. Like many other programming languages, Python has the following types of loops.
- for in
- while
for in
# Print ten numbers using for in loop
for i in range(0, 10, 1):
print(i)
# Iterate over the given list & print the content
shopping_list = ['Processor', 'RAM', 'Motherboard']
for item in shopping_list:
print(item)
# Processor, RAM, Motherboard
while
# Print ten numbers using while loop
i = 0
while(i < 10):
print(i)
i += 1
# Iterate over the given list & print the content
shopping_list = ['Processor', 'RAM', 'Motherboard']
size = len(shopping_list)
i = 0
while(i < size):
print(shopping_list[i])
i += 1
# Processor, RAM, Motherboard
Functions
Object oriented programming languaes are mainly comprised of modular chunks of code which make it easy to maintain & most importantly to debug. Functions are one way to achieve modularity in Python. Functions are blocks of code which perform certain tasks & yield a result.
In Python, the def keyword is
used to define a function's prototype. For Example:
# Function to compute the area of rectangle
def area(length, breadth):
return(length * breadth)
print(area(20, 25))
# 500