List comprehensions
List comprehensions are expressions that allow us to transform lists in a single line. The syntax:
It may look intimidating at first glance but what will happen is python loops through the list and executes the
Above we have two expression the
#PythonTips #GettingToKnowPython #PythonPatterns
List comprehensions are expressions that allow us to transform lists in a single line. The syntax:
[ <inner_expression> for <item> in <list> ]
It may look intimidating at first glance but what will happen is python loops through the list and executes the
<inner_expression> for each <item> in <list> to create a new list. # Example
nums = [1, 2, 3, 4, 5]
squared = [n ** 2 for n in nums]
print(squared)
# output: [1, 4, 9, 16, 25]
Above we have two expression the
outer_expression which is the list comprehension itself and the inner_expression (n ** 2) which transforms the list. In this case the inner_expression squares each item from the list.#PythonTips #GettingToKnowPython #PythonPatterns
β€1
Please open Telegram to view this post
VIEW IN TELEGRAM
π3
Lambda functions
Another cool expressions or patterns in python are lambda functions a.k.a anonymous functions. We use them to define functions inline (in one line).
The subtle thing we need to understand about
The concept actually exists in many programming languages as
#LambdaFunctions #PythonProgramming #PythonTips #GettingToKnowPython
Another cool expressions or patterns in python are lambda functions a.k.a anonymous functions. We use them to define functions inline (in one line).
# Syntax
lambda arguments: expresion
The subtle thing we need to understand about
anonymous funtions/lambda functions is that they don't have a name unless we explicitly assign them to a variable. The return value of lambda function is the evaluated expression.The concept actually exists in many programming languages as
anonymous functions or function expression, even-though the syntax could be different.# Examples
# This is valid - With no name
lambda x: x
# With a name
times2 = lambda x: x * 2
# With two argumetns
prefix = lambda pre, post: pre + post;
postfix = lambda post, pre: prefix(pre, post)
print(times2(10))
print(prefix("main", ".py"))
print(postfix(".py", "main"))
# ----------Output----------
20
main.py
main.py
#LambdaFunctions #PythonProgramming #PythonTips #GettingToKnowPython
β€1
Please open Telegram to view this post
VIEW IN TELEGRAM
Python Pioneers: A Beginner's Guide πͺπΉ
Explanation
Happy coding π
# given data
words = ["to code", "python", "linux"]
# func - prefixes any given string with 'I love '
func = lambda word: "I love " + word
# transforms every item using - func
results = [func(w) for w in words]
# results
# ["I love to code", "I love python", "I love linux"]
# results[1] - is the second item
# Remember indexes start from 0 not 1
print(results[1])
# Correct Answer: "I love python" (option 1)
Happy coding π
β‘3β€1
We now have a new community group to discuss on different topics
You can also dm additional topics to be created
https://t.me/python_pioneers_com
You can also dm additional topics to be created
https://t.me/python_pioneers_com
π2
OOP in Python
Object Oriented Programming (OOP) just means we organize code using objects that have Data and Actions. Data is something we know about the object, and Actions are things we can do to the object or with the object.
For example, when looking at a computer, we may find ourselves asking the brand, model and specs of a computer, these are some of the data(information) we can have about a computer. Also we can do things on a computer; power it On & Off, Open & Close apps, adjust the volume - these are some of the action we can take on a computer.
Object Oriented Programming (OOP) just means we organize code using objects that have Data and Actions. Data is something we know about the object, and Actions are things we can do to the object or with the object.
For example, when looking at a computer, we may find ourselves asking the brand, model and specs of a computer, these are some of the data(information) we can have about a computer. Also we can do things on a computer; power it On & Off, Open & Close apps, adjust the volume - these are some of the action we can take on a computer.
Note: In the context of programming we usually say Attribute or Property instead of Data and Method instead of Action.
β€2
...continued
To do OOP in python we use classes. Classes define the shape (attributes and methods) of an object. Think of classes like a blueprint for creating objects. Next we use classes to create objects.
It's a simple two step process:-
β’ Modeling:- defining a class (shape).
β’ Instantiating:- creating objects (instances) using class.
Syntax: to create a class we use the
Note:
β’ Attributes can be of any data type.
β’ Methods are just functions attached to the object.
β’
To do OOP in python we use classes. Classes define the shape (attributes and methods) of an object. Think of classes like a blueprint for creating objects. Next we use classes to create objects.
It's a simple two step process:-
β’ Modeling:- defining a class (shape).
β’ Instantiating:- creating objects (instances) using class.
Syntax: to create a class we use the
class keyword.# Step 1 - Modeling
class ClassName:
attr1 = 0
attr2 = True
attr3 = "string"
def method1(self): pass
def method2(self): pass
# Step 2 - Instantiating
obj1 = ClassName()
obj2 = ClassName()
Note:
β’ Attributes can be of any data type.
β’ Methods are just functions attached to the object.
β’
self is a reference to the object; obj1 or obj2 (we will talk more about them later)...continued
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
Example: simplified computer object
#oop #PythonProgramming #DotNotation
To access attributes or call methods on an object we use a dot-notation., the variable followed by dot and the attribute or the method name.
Syntax: the dot-notation
# Accessing an attribute
object.attribute
# Calling a method
object.method()
Example: simplified computer object
class Computer:
# __init__ is a special method (a constructor)
def __init__(self, brand, model):
# attributes
self.brand = brand
self.model = model
# methods
def powerOn(self):
print(f"Powering on {self.brand} {self.model}")
def powerOff(self):
print(f"Powering off {self.brand} {self.model}")
c1 = Computer("HP", "OMEN")
c2 = Computer("Dell", "XPS")
# Test code
print(c2.brand + " " + c2.model)
c1.powerOn()
# Output
Dell XPS
Powering on HP OMEN
#oop #PythonProgramming #DotNotation
β€1
In Python's object-oriented programming, what are the official terms used to describe an object's data and its actions?
Anonymous Quiz
45%
Variables and Functions
14%
Fields and Actions
33%
Attributes and Methods
8%
Properties and Routines
Please open Telegram to view this post
VIEW IN TELEGRAM
Discover a Smarter Way to Learn & Share Knowledge
As a ScholarXiv Ambassador, Iβm excited to introduce you to ScholarXiv β a platform built for students, researchers, academics, and lifelong learners who want to discover and share scholarly knowledge.
Whether youβre working on an assignment, research project, thesis, or simply exploring a new field, ScholarXiv can help you connect with academic resources and ideas.
π¬ Research. Learn. Share. Connect.
If youβre serious about your academic journey, take a look at ScholarXiv and see what it has to offer.
π Explore: scholarxiv.com
As a ScholarXiv Ambassador, Iβm excited to introduce you to ScholarXiv β a platform built for students, researchers, academics, and lifelong learners who want to discover and share scholarly knowledge.
Whether youβre working on an assignment, research project, thesis, or simply exploring a new field, ScholarXiv can help you connect with academic resources and ideas.
π¬ Research. Learn. Share. Connect.
If youβre serious about your academic journey, take a look at ScholarXiv and see what it has to offer.
π Explore: scholarxiv.com
β€2