Telegram Web
⚠️ Daily Python Facts ⚠️

The pow(x, y) function returns the value of x to the power of y (xy).

x = pow(2, 3)
print(x)


Output: 8

- @pythonpool -
⚠️ Daily Python Facts ⚠️

The abs() function returns the absolute (positive) value of the specified number:

x = abs(-7.25)
print(x)

Output: 7.25

- @pythonpool -
⚠️ Daily Python Facts ⚠️

Error Handling. When execuatable code in try fails, except is executed and finally is definatelly executes.

try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

Output:
Something went wrong
The 'try except' is finished


- @pythonpool -
⚠️ Python Daily Facts ⚠️

Python to JSON

import json

# a Python object (dict):
x = {
"name": "John",
"age": 30,
"city": "New York"
}

# convert into JSON:
y = json.dumps(x)

# the result is a JSON string:
print(y)


Output:
{"name": "John", "age": 30, "city": "New York"}

- @pythonpool -
Want to learn Python? We've got a perfect app for you.

Pycoders - Learn Python

Features -
- Video Lectures
- Fun Learning python course
- Quizzes
- Mini projects
- 100+ Interview questions
- Forum to discuss

https://play.google.com/store/apps/details?id=com.karan.pycoders
⚠️ Daily Python Facts ⚠️

Package Mangaer or PIP to use external libraries.

Install PIP
If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/

Download a Package
Example-Download a package named "camelcase" from CMD:
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip install camelcase

Using a Package
Once the package is installed, it is ready to use.
Import the "camelcase" package into your project.


import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))


Output: Hello World

- @pythonpool -
⚠️ Python Daily Facts ⚠️

Unpack variables from iterable without indicating all elements.

>>> a, *b, c = [1, 2, 3, 4, 5]
>>> a
1
>>> b
[2, 3, 4]
>>> c
5


- @pythonpool -
⚠️ Daily Python Facts ⚠️

String Formatting

quantity = 3
itemno = 567
price = 49
myorder = "I want {} pieces of item number {} for {:.2f} dollars."
print(myorder.format(quantity, itemno, price))


Output:
I want 3 pieces of item number 567 for 49.00 dollars.

- @pythonpool -
⚠️ Daily Python Facts ⚠️

Python is more like English

Many people say that Python is easy to read the language. The main reason for this claim is Python is more like English. You can easily understand what every line of code is doing. Everything is straight forward and direct to the point.

- @pythonpool -
⚠️ Daily Python Facts ⚠️

Python does not require a compiler

As a high-level and interpreted language, Python does not need a compiler. This is unlike Java and C++ which have to be compiled first before being interpreted. For Python, it relies on an application known as an interpreter.
The Python byte code is stored in the form of a .pyc file which is then executed by an appropriate virtual machine. This machine acts as a run-time engine of Python.

- @pythonpool -
⚠️ Daily Python Facts ⚠️

Python’s special Slice Operator.

# Slice Operator
a = [1,2,3,4,5]

print(a[0:2]) # Choose elements [0-2), upper-bound noninclusive

print(a[0:-1]) # Choose all but the last

print(a[::-1]) # Reverse the list

print(a[::2]) # Skip by 2

print(a[::-2]) # Skip by -2 from the back

Output:
[1, 2]
[1, 2, 3, 4]
[5, 4, 3, 2, 1]
[1, 3, 5]
[5, 3, 1]


- @pythonpool -
⚠️ Daily Python Facts ⚠️

To execute a statemnt when a For loop ends naturally.

def func(array):
for num in array:
if num%2==0:
print(num)
break # Case1: Break is called, so 'else' wouldn't be executed.
else: # Case 2: 'else' executed since break is not called
print("No call for Break. Else is executed")

print("1st Case:")
a = [2]
func(a)
print("2nd Case:")
a = [1]
func(a)


Output: 1st Case:
2
2nd Case:
No call for Break. Else is executed


- @pythonpool -
⚠️ Daily Python Facts ⚠️

Combining two iterables of tuples with padding or pivot nested iterable with padding.

>>> import itertools as it
>>> x = [1, 2, 3, 4, 5]
>>> y = ['a', 'b', 'c']
>>> list(zip(x, y))
[(1, 'a'), (2, 'b'), (3, 'c')]

>>> list(it.zip_longest(x, y))
[(1, 'a'), (2, 'b'), (3, 'c'), (4, None), (5,None)]


- @pythonpool -
⚠️ Daily Python Facts ⚠️

One can chain comparison operators in Python

# Chaining Comparison Operators
i = 5;

ans = 1 < i < 10
print(ans)

ans = 10 > i <= 9
print(ans)

ans = 5 == i
print(ans)

Output:
True
True
True


- @pythonpool -
⚠️ Daily Python Facts ⚠️

Ordered dict structure (a subclass of dictionary that keeps order).

>>> from collections import OrderedDict
>>> d = OrderedDict.fromkeys('abcde')
>>> d.move_to_end('b')
>>> ''.join(d.keys())
'acdeb'

>>> d.move_to_end('b', last=False)
>>> ''.join(d.keys())
'bacde'


- @pythonpool -
⚠️ Daily Python Facts ⚠️

Dict comprehension.

>>> m = {x: x ** 2 for x in range(5)}
>>> m
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}


- @pythonpool -
⚠️ Daily Python Facts ⚠️

An elegant way to deal with a file path (3.4≥)

>>> from pathlib import Path
>>> data_folder = Path("source_data/text_files/")

# Path calculation and metadata
>>> file_to_open = data_folder / "raw_data.txt"
>>> file_to_open.name
"raw_data.txt"
>>> file_to_open.suffix
"txt"
>>>file_to_open.stem
"raw_data"

# Files functions
>>> f = open(file_to_open)
>>> f.read()
# content of the file
>>> file_to_open.exists()
True


- @pythonpool -
⚠️ Daily Python Facts ⚠️

Display current Date and Time

import datetime
x = datetime.datetime.now()
print(x)


- @pythonpool -
⚠️ Daily Python Facts ⚠️

It is also possible to use the dict() constructor to make a new dictionary

thisdict = dict(brand="Ford", model="Mustang", year=1964)
# note that keywords are not string literals
# note the use of equals rather than colon for the assignment
print(thisdict)

Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}


- @pythonpool -
⚠️ Daily Python Facts ⚠️

It is also possible to use the set() constructor to make a set.

thisset = set(("apple", "banana", "cherry"))
print(thisset)

Output:
{'apple', 'banana', 'cherry'}


- @pythonpool -
2025/07/13 11:03:20
Back to Top
HTML Embed Code: