name = "Python Language" if (pos = name.find(" ")) >= 0: print(f"Found space at : {pos}") else: print("Found no space!")
if (pos = name.find(" ")) >= 0: ^ SyntaxError: invalid syntax
name = "Python Language" if (pos := name.find(" ")) >= 0: print(f"Found space at : {pos}") else: print("Found no space!")
names = [] while (name := input("Enter your name [end to stop] : ")) != 'end': names.append(name) print(sorted(names))
def add(a, b, /): return a + b print(add(10, 20))
print(add(a=20, b=20))
TypeError: add() got some positional-only arguments passed as keyword arguments: 'a, b'
name = "Python" print(f"{name}") print(f"{name=}")
Python name='Python'
pip install mypy
def add(a: int, b: int) -> int: return a + b print(add(10,20)) print(add("Xyz","Abc"))
C:\dev\python>mypy hints_demo.py hints_demo.py:6: error: Argument 1 to "add" has incompatible type "str"; expected "int" hints_demo.py:6: error: Argument 2 to "add" has incompatible type "str"; expected "int" Found 2 errors in 1 file (checked 1 source file)
# File : final_demo.py from typing import final from typing import Final num : Final = 100 num = 10 # Error num cannot be changed @final class Person: def __init__(self,name,email): self.name = name self.email = email class Student(Person): # Error Person cannot be inherited pass
C:\dev\python>mypy final_demo.py final_demo.py:6: error: Cannot assign to final name "num" final_demo.py:14: error: Cannot inherit from final class "Person" Found 2 errors in 1 file (checked 1 source file)
from typing import Literal def print_message(message : str, case : Literal['u','l'] = 'u'): if case == 'u': print(message.upper()) else: print(message.lower()) print_message("Hello") print_message("Hello","n")
C:\dev\python>mypy literal_hint.py literal_hint.py:11: error: Argument 2 to "print_message" has incompatible type "Literal['n']"; expected "Union[Literal['u'], Literal['l']]" Found 1 error in 1 file (checked 1 source file)