Interpreter Expressions

I LOVE DOING THIS :)

Expressions are representations of values, they are totally different from statements and functions since they perform any action, Meanwhile expressions are just the representation

Create an Expression

They only contain Identifiers, Literals, and Operators

  • Any name that is used to define a class, function, variable module, or object is an Identifier

  • There are the string literals, byte literals, integer literals, floating point literals, and imaginary literals, basically which means the independent usage of data types

  • Operators are nothing but + - * / % > < >= <= == >> <<

List Expression

One liner syntax - for loop

>>> [x for x in range(10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Dictionary Expression

One liner syntax - for loop

{x:x*2 for x in range(5)}
{0: 0, 1: 2, 2: 4, 3: 6, 4: 8}

Generator Expression

One liner syntax - for loop

Will initialize a generator object that returns the values within 5 when the object is called

>>> d0p = (x for x in range(5))
<generator object <genexpr> at 0x7fec47aee870>
>>> list(d0p)
[0, 1, 2, 3, 4]

Conditional Expressions

true_value if Condition else false_value

>>> x = "1" if True else "2"
>>> x
'1'

Last updated