Python : Weird Codes


Python Weird Codes.

An effective way to learn programming is to write weird code. In this way, you can reason deeply about the principles of programming languages.



Difference between Keywords and Built-in functions


[ Keywords ]

[ Built-in functions ]


Python keywords are special reserved words that have specific meanings and purposes and can’t be used for anything but those specific purposes. These keywords are always available.


Python keywords are different from Python’s built-in functions and types. The built-in functions and types are also always available, but they aren’t as restrictive as the keywords in their usage.


Let's study above topic using example code.



Some more on Keywords: can you find erroneous part?


# Calculate the sum and average of grades.
course_grade = [98, 100, 85, 89, 93]

sum = 0
for course in course_grade:
    sum += course

print("course sum:", sum)
print("average:", sum / len(course_grade))

# doing same job using built-in function
print("course sum:", sum(course))
print("average:", sum(course) / len(course_grade))



Float numbers

Inside a computer, float data cannot be accurately represented. Accurate calculation is possible only if you know exactly why.

Look at the code below. If you think based on what you learned in math class, it should output the same result, but it isn't. Why?


# What will be printed out?

print(0.2 == 0.2 * 3 / 3)
print(0.2 == 0.2 * (3 / 3))


Short-curcuit evaluation


# What will be printed out?

def test1():
    print("Test1 called")
    return True


def test2():
    print("Test2 called")
    return False

if test1() or test2():
    print("Or operator")
else:
    print("Or operator False")

Short-circuiting is the stoppage of execution of boolean operation if the truth value of expression has been determined already. The evaluation of expression takes place from left to right. In python, short-circuiting is supported by various boolean operators and functions. 

Let's study above topic using example code.



Comments

To write a comment in Python, simply put the hash mark # before your desired comment:

# This is a comment
"""
This is not a comment.
This is just a string literal.
"""
Python interpreter ignores everything after the hash mark and up to the end of the line. You can insert them anywhere in your code, even inline with other code:

Let's study above topic using example code.


Difference between 'is' keyword  and '==' operator

The == operator compares the value of two objects, whereas the Python is keyword checks whether two objects are same object in memory.


# Guess What?

a = 1000
print(a == 1000)

print(a is 1000)
print(id(1000), id(a))

Let's study above topic using example code.


False literals in Python

# True ?  False ?

print(True and True)
print(1 and 2)
print(True and 1)
print(1 and True)

Printing Binary numbers

# Understand what these means?

print(0b0101)
print(-0b0101)

a = 0b0101
print(a)

a = -0b0101
print(a)
Let's study above topic using example code.


Python has OverFlow?

# Overflow happens?

for_checking_overflow = 1

while True:
    for_checking_overflow  *= 1000
    print(for_checking_overflow)
Let's study above topic using example code.


In Python, print( ) function is expression?

a = print(10)

# what will be printed out?
print(a)

Operators

# Operators
- unary operators
- binary operators
- ternary operators





Comments

Popular posts from this blog

Python (Level-1): Introductions

Python (Level-3): Data Types

Processing Language(Level-1): Game Coding.