Files
python-intermediate/intermediate_course_repl_playaround.ipynb
T

37 KiB

In [ ]:
from __future__ import braces
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1
SyntaxError: not a chance

In [ ]:
import antigravity
In [ ]:
a = "Aman, Nalakath"
In [ ]:
first_name, last_name = a.split(",")
In [ ]:
last_name
 Nalakath
In [ ]:
name_raw = a.split(",")
In [ ]:
full_name = (",").join(name_raw)
full_name
In [ ]:
full_name
Aman, Nalakath
In [ ]:
name_raw
['Aman', ' Nalakath']
In [ ]:
full_name_with_an_x = ("_x ").join(name_raw)
In [ ]:
full_name_with_an_x
Aman_x  Nalakath
In [ ]:
int("123")
123
In [ ]:
list("aman")
['a', 'm', 'a', 'n']
In [ ]:
"".join(list("aman"))
aman
In [ ]:
type("".join(list("aman")))
<class 'str'>
In [ ]:
names = "aman, shamil, aman, nalakath"
In [ ]:
set(names)
{'i', ' ', 'a', 'k', 'm', 'n', 's', 'h', 'l', 't', ','}
In [ ]:
names = ["aman", "shamil", "aman", "nalakath"]
In [ ]:
set(names)
{'aman', 'nalakath', 'shamil'}
In [ ]:
list(set(names))
['aman', 'nalakath', 'shamil']
In [ ]:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [ ]:
for num in numbers:
    if num % 2 == 0:
        print(num ** 2)
4
16
36
64
100
In [ ]:
[print(num ** 2) for num in numbers if num % 2 == 0]
4
16
36
64
100
[None, None, None, None, None]
In [ ]:
n_a = [(num ** 2) for num in numbers if num % 2 == 0]
In [ ]:
print(n_a)
[4, 16, 36, 64, 100]
In [ ]:
range(0, 10, 2)
range(0, 10, 2)
In [ ]:
list(range(0, 10, 2))
[0, 2, 4, 6, 8]
In [ ]:
["length: len(i)" for i in list(set(names))]
['length: len(i)', 'length: len(i)', 'length: len(i)']
In [ ]:
[f"length: len(i)" for i in list(set(names))]
['length: len(i)', 'length: len(i)', 'length: len(i)']
In [ ]:
list(set(names))
['aman', 'nalakath', 'shamil']
In [ ]:
type(list(set(names)))
<class 'list'>
In [ ]:
[("length": len(name)) for name in list(set(names))]
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1
    [("length": len(name)) for name in list(set(names))]
              ^
SyntaxError: invalid syntax

In [ ]:
[("length:" len(name)) for name in list(set(names))]
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1
    [("length:" len(name)) for name in list(set(names))]
      ^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

In [ ]:
[("length:",len(name)) for name in list(set(names))]
[('length:', 4), ('length:', 8), ('length:', 6)]
In [ ]:
n_a = [(num ** 2) for num in numbers if num % 2 == 0]
In [ ]:
n_a = [(num ** 2) for num in numbers if num % 2 == 0]
print(n_a)
[4, 16, 36, 64, 100]
In [ ]:
n_b = [(num ** 2) for num in numbers if bool(num % 2)]
print(n_b)
[1, 9, 25, 49, 81]
In [ ]:
n_c = sorted([(num ** 2) for num in numbers if num % 2 == 0], reverse=True)
print(n_c)
[100, 64, 36, 16, 4]
In [ ]:
n_c = sorted([(num ** 2) for num in range(0, 50) if num % 2 == 0], reverse=True)
print(n_c)
[2304, 2116, 1936, 1764, 1600, 1444, 1296, 1156, 1024, 900, 784, 676, 576, 484, 400, 324, 256, 196, 144, 100, 64, 36, 16, 4, 0]
In [ ]:
type{1}
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1
    type{1}
        ^
SyntaxError: invalid syntax

In [ ]:
type({1})
<class 'set'>
In [ ]:
type({1:1})
<class 'dict'>
In [ ]:
numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [ ]:
numbers[::-1]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
In [ ]:
names = ["Bob", "Alice", "Eve"]
In [ ]:
score_dict = dict(zip(names, numbers[::-1]))
print(score_dict)
{'Bob': 10, 'Alice': 9, 'Eve': 8}
In [ ]:
(num for num in range[0:100])
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1, in <module>
TypeError: type 'range' is not subscriptable

In [ ]:
(num*2 for num in range(0, 100))
<generator object <genexpr> at 0x100e96e90>
In [ ]:
next((num*2 for num in range(0, 100)))
0
In [ ]:
next((num*2 for num in range(0, 100)))
0
In [ ]:
next((num*2 for num in range(0, 100)))
0
In [ ]:
next((num*2 for num in range(0, 100)))
next((num*2 for num in range(0, 100)))
next((num*2 for num in range(0, 100)))
In [ ]:
print(next((num*2 for num in range(0, 100))))
0
In [ ]:
print(next((num*2 for num in range(0, 100))))
print(next((num*2 for num in range(0, 100))))
0
0
In [ ]:
times_two = (num*2 for num in range(0, 100))
In [ ]:
next(times_two)
next(times_two)

In [ ]:
next(times_two)
4
In [ ]:
next(times_two)
6
In [ ]:
next(times_two)
8
In [ ]:
next(times_two, "stopped")
10
In [ ]:
names = ["Alice", "Bob", "Charlie", "Diana"]
ages = [28, 34, 22, 41]
cities = ["Berlin", "Paris", "Tokyo", "London"]
scores = [88, 95, 72, 90]
In [ ]:
name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1
    name, age, city, score = [for name, age, city, score in zip(names, ages, cities, scores)]
                              ^^^
SyntaxError: invalid syntax

In [ ]:
name, age, city, score = [f"{name}, age {age}, from {city}, scored {score}" for name, age, city, score in zip(names, ages, cities, scores)]
In [ ]:
print(name, age, city, score)
Alice, age 28, from Berlin, scored 88 Bob, age 34, from Paris, scored 95 Charlie, age 22, from Tokyo, scored 72 Diana, age 41, from London, scored 90
In [ ]:
name, age, city, score = [f"{name}, age {age}, from {city}, scored {score}\n" for name, age, city, score in zip(names, ages, cities, scores)]
In [ ]:
print(name, age, city, score)
Alice, age 28, from Berlin, scored 88
 Bob, age 34, from Paris, scored 95
 Charlie, age 22, from Tokyo, scored 72
 Diana, age 41, from London, scored 90

In [ ]:
name, age, city, score = [f"{name} age {age} from {city} scored {score}\n" for name, age, city, score in zip(names, ages, cities, scores)]
In [ ]:
print(name, age, city, score)
Alice age 28 from Berlin scored 88
 Bob age 34 from Paris scored 95
 Charlie age 22 from Tokyo scored 72
 Diana age 41 from London scored 90

In [ ]:
new_list = range(20)
In [ ]:
import random
In [ ]:
{name: random.randint(20, 40)}
{'Alice age 28 from Berlin scored 88\n': 23}
In [ ]:
age
Bob age 34 from Paris scored 95

In [ ]:
city
Charlie age 22 from Tokyo scored 72

In [ ]:
names = ["Alice", "Bob", "Charlie", "Diana"]
ages = [28, 34, 22, 41]
cities = ["Berlin", "Paris", "Tokyo", "London"]
scores = [88, 95, 72, 90]
In [ ]:
{name: random.randint(20, 40)}
{'Alice age 28 from Berlin scored 88\n': 32}
In [ ]:
{names: random.randint(20, 40)}
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1, in <module>
TypeError: unhashable type: 'list'

In [ ]:
{name: random.randint(20, 40) for name in names}
{'Alice': 37, 'Bob': 24, 'Charlie': 27, 'Diana': 31}
In [ ]:
True + True
2
In [ ]:
{1, 4, True, True, 0, False}
{0, 1, 4}
In [ ]:
raise Exception(message = "trying this out, don't mind me")
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1, in <module>
TypeError: Exception() takes no keyword arguments

In [ ]:
raise Exception("trying this out, don't mind me")
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 1, in <module>
Exception: trying this out, don't mind me

In [ ]:
try:
    int("this is not a number")

except ValueError as e:
    f"Damn you got a {e}, be more careful, it is not a number. check again"
In [ ]:
try:
    int("this is not a number")

except ValueError as e:
    print(f"Damn you got a {e}, be more careful, it is not a number. check again")
Damn you got a invalid literal for int() with base 10: 'this is not a number', be more careful, it is not a number. check again
In [ ]:
list(range(10,-1,-1))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In [ ]:
import time
for item in list(range(10,-1,-1)):
    time.sleep(1)
    print(item)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import time
for item in next(range(10,-1,-1)):
    time.sleep(1)
    print(item)
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 2, in <module>
TypeError: 'range' object is not an iterator

In [ ]:
import time
for item in next(list(range(10,-1,-1))):
    time.sleep(1)
    print(item)
Traceback (most recent call last):
  File "/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py", line 139, in exec_user_input
    retval = callable_(user_input, user_globals)
  File "<string>", line 2, in <module>
TypeError: 'list' object is not an iterator

In [ ]:
import time
for item in range(10,-1,-1):
    print(item)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import time
for item in range(10,-1,-1):
    print(item)
    time.sleep(1)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import time
for item in range(10, -1, -1):
    print(item)
    if item > 0:
        time.sleep(1)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import time
for item in range(10,-1,-1):
    print(item, flush=True)
    time.sleep(1)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import time
for item in range(10, -1, -1):
    print(item, flush=True)
    time.sleep(1)
10
9
8
7
6
5
4
3
2
1
0
In [ ]:
import os
with os.scandir(os.getcwd()) as folder:
    for entry in folder:
        print(f"-- {entry}")
-- <DirEntry 'home'>
-- <DirEntry 'usr'>
-- <DirEntry '.resolve'>
-- <DirEntry 'bin'>
-- <DirEntry 'sbin'>
-- <DirEntry '.file'>
-- <DirEntry 'etc'>
-- <DirEntry 'var'>
-- <DirEntry 'Library'>
-- <DirEntry 'System'>
-- <DirEntry '.VolumeIcon.icns'>
-- <DirEntry 'private'>
-- <DirEntry '.vol'>
-- <DirEntry 'Users'>
-- <DirEntry 'Applications'>
-- <DirEntry 'opt'>
-- <DirEntry 'dev'>
-- <DirEntry 'Volumes'>
-- <DirEntry '.nofollow'>
-- <DirEntry 'tmp'>
-- <DirEntry 'cores'>
In [ ]:
import os
with os.scandir(os.getcwd()) as folder:
    for entry in folder:
        print(f"-- {entry.name}")
-- home
-- usr
-- .resolve
-- bin
-- sbin
-- .file
-- etc
-- var
-- Library
-- System
-- .VolumeIcon.icns
-- private
-- .vol
-- Users
-- Applications
-- opt
-- dev
-- Volumes
-- .nofollow
-- tmp
-- cores
In [ ]:
import sys
sys.platform
In [ ]:
import sys
print(sys.platform)
darwin
In [ ]:
print(f"working with these arguments {sys.argv}")
working with these arguments ['/Users/aman/.vscode/extensions/ms-python.python-2026.4.0-darwin-arm64/python_files/python_server.py']
In [ ]:
type(sys.argv)
<class 'list'>
In [ ]: