Do you know any other interesting features about coding in Python?

Joined
Jul 4, 2023
Messages
366
Reaction score
41
At the beginning of ...

[ on-line ]
Python:
features = 'PyTHoN'

print(f'{features:*<40}')
print(f'{features:*>40}')
print(f'{features:*^40}')

for i in range(len(features)+1,41):
    print(f'{features:*>{i}}')

[ on-line ] ;)
Python:
tuple_ = (1,2,3)
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = 1,2,3
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = 1,
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = (1,)
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = (1)
print(type(tuple_)) # <class '?'>

tribute to menator01 idea
[ on-line ]
Python:
from time import sleep

string = 'This is a string'
def stars(arg): 
    return [print('*', end='', flush=True) or sleep(.15) for _ in range(len(arg))]


[(lambda s: (stars(s), print(f'\n{s}'), stars(s)))(string)]

please, share what do you know ...
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
At the beginning of ...

[ on-line ]
Python:
features = 'PyTHoN'

print(f'{features:*<40}')
print(f'{features:*>40}')
print(f'{features:*^40}')

for i in range(len(features)+1,41):
    print(f'{features:*>{i}}')

[ on-line ] ;)
Python:
tuple_ = (1,2,3)
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = 1,2,3
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = 1,
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = (1,)
print(type(tuple_)) # <class 'tuple'>
 
tuple_ = (1)
print(type(tuple_)) # <class '?'>

tribute to menator01 idea
[ on-line ]
Python:
from time import sleep

string = 'This is a string'
def stars(arg):
    return [print('*', end='', flush=True) or sleep(.15) for _ in range(len(arg))]


[(lambda s: (stars(s), print(f'\n{s}'), stars(s)))(string)]

please, share what do you know ...

Dont know if you know about him. But I recently saw a youtube video by Tech with Tim. The video was called python is weird and he featured a lot of interesting code snippets like this.
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Credit to Tech with Tim...
Code:
import this
Python:
a = ''.join(['o', 'k', 'a', 'y'])
b = 'o' + 'k' + 'a' + 'y'
c = 'okay'

print(a is b)
print(a is c)
print(b is c)
Python:
print((1 == 1) in [1])
print(1 == (1 in [1]))
print(1 < (0 <1))
print(1 < 0 < 1)
Python:
print(all([[]]))
print(all([[[]]]))
Python:
a, b = a[b] = {}, 5
print(a, b)
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Easy and fast way to copy list ... (a shallow copy with a full slice of the original)
Python:
orginal_list = [ 'A','2','3','4','5','6','7' ]
copied_list = orginal_list[:]

[ on-line ] (a very simple example ;) )
Card deck entropy, in the context of shuffling, refers to the degree of disorder or unpredictability in the arrangement of cards in a deck. It measures how difficult it is to predict the order of cards after shuffling. Higher entropy indicates a more random and unpredictable card distribution, which is essential for fair gameplay in card games. Entropy is a fundamental concept in information theory and statistics, helping to quantify the level of uncertainty in a system. In card games, higher entropy means more secrecy about card positions, enhancing the element of surprise during play.
Python:
import random
import time


orginal_list = [ 'A','2','3','4','5','6','7' ]
shuffle_list = orginal_list[:]
random.shuffle(shuffle_list)
counter = 0

while not orginal_list == shuffle_list:
    random.shuffle(shuffle_list)
    counter += 1
    print(counter, shuffle_list)
    time.sleep(0.05)

print(f'\nAfter {counter} draws, the positions on both lists are in the same order again.')
input('\nENTER...')
 
Joined
Jul 4, 2023
Messages
366
Reaction score
41
Python:
Code:
a = ''.join(['o', 'k', 'a', 'y'])
b = 'o' + 'k' + 'a' + 'y'
c = 'okay'

print(a is b)
print(a is c)
print(b is c)

[ on-line ]
Python:
a = ''.join(['o', 'k', 'a', 'y'])
b = 'o' + 'k' + 'a' + 'y'
c = 'okay'

print('a', type(a), 'b', type(b), 'c', type(c))

print('\nis')
print(a is b)
print(a is c)
print(b is c)

print('\n==')
print(a == b)
print(a == c)
print(b == c)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top