Python is a widely used general-purpose, high level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more efficiently.
There are two major Python versions: Python 2 and Python 3. Both are quite different.
A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. In Python, everything is a object.
Decorator takes a function as arguments and returns a function by adding some functionality.
def our_decorator(func):
def function_wrapper(x):
print("Before calling " + func.__name__)
func(x)
print("After calling " + func.__name__)
return function_wrapper
@our_decorator
def foo(x):
print("Hi, foo has been called with " + str(x))
foo("Hi")
Note:
@
is used to call the decorated function more eligantly.
Decorator closely related to some python concepts which are basic building block for decorators.
Details also available here Nested Functions
We can include one function inside another, known as a nested function. For example,
def outer(x):
def inner(y):
return x + y
return inner
add_five = outer(5)
result = add_five(6)
print(result) # prints 11
# Output: 11
Pass Functions as Arguments
We can pass a function as an argument to another function in Python. For Example,
def add(x, y):
return x + y
def calculate(func, x, y):
return func(x, y)
result = calculate(add, 4, 6)
print(result) # prints 10
Return a Function as a Value
we can also return a function as a return value. For example,
def greeting(name):
def hello():
return "Hello, " + name + "!"
return hello
greet = greeting("Atlantis")
print(greet()) # prints "Hello, Atlantis!"
# Output: Hello, Atlantis!
Before Decorator @
symbol
def make_pretty(func):
# define the inner function
def inner():
# add some additional behavior to decorated function
print("I got decorated")
# call original function
func()
# return the inner function
return inner
# define ordinary function
def ordinary():
print("I am ordinary")
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
# call the decorated function
decorated_func()
After Decorator @
symbol
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
@make_pretty
def ordinary():
print("I am ordinary")
ordinary()
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 classes and variables are instances (objects) of these classes.
The following are the standard or built-in data types in Python:
1. Numbers – They include integers, floating-point numbers, and complex numbers. eg. 1, 7.9,3+4i
2. List – An ordered sequence of items is called a list. The elements of a list may belong to different data types. Eg. [5,’market’,2.4]
3. Tuple – It is also an ordered sequence of elements. Unlike lists , tuples are immutable, which means they can’t be changed. Eg. (3,’tool’,1)
4. String – A sequence of characters is called a string. They are declared within single or double-quotes. Eg. “Sana”, ‘She is going to the market’, etc.
5. Set – Sets are a collection of unique items that are not in order. Eg. {7,6,8}
6. Dictionary – A dictionary stores values in key and value pairs where each value can be accessed through its key. The order of items is not important. Eg. {1:’apple’,2:’mango}
7. Boolean – There are 2 boolean values - True and False.
# Numbers Data Types
a = 5
print("Type of a: ", type(a))
b = 5.0
print("\nType of b: ", type(b))
c = 2 + 4j
print("\nType of c: ", type(c))
# Sequence Types
# String Types
String1 = 'Welcome to the Geeks World'
print("String with the use of Single Quotes: ")
Note:
type()
function is used to determine the type of data type.
- How is memory managed in Python?
Ans: Memory is managed in Python in the following ways:
- Q29.What is a lambda function?
Ans: An anonymous function is known as a lambda function. This function can have any number of parameters but, can have just one statement.
Example:
a = lambda x,y : x+y
print(a(5, 6))
Q3: What are the generators in python?
Ans: Functions that return an iterable set of items are called generators.
Q4. What does this mean: *args, **kwargs? And why would we use it?
*args
when we aren’t sure how many arguments are going to be passed to a function, or if we want to pass a stored list or tuple of arguments to a function.
**kwargs
is used when we don’t know how many keyword arguments will be passed to a function, or it can be used to pass the values of a dictionary as keyword arguments. The identifiers args and kwargs are a convention, you could also use *bob and **billy but that would not be wise.
Q5. What are Python packages?
.py
extension__init__.py
file.