Skip to content

Choaib-ELMADI/every-python-punctuation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Every Punctuation in Python

  • Backtick `: Doesn't do anything in Python.


  • Tilde ~: Bitwise Not operator.

x = 100
print(~x)
>> -101

  • Exclamation Mark !: Doesn't do anything on it's own. Combining it with = makes the not equality operator.

x = 4
if x != 5:
    print("x is not equal to 5")

  • Question Mark ?: Doesn't do anything in Python.


  • At Sign @: Its main usage is in decorators.

import time


def tictoc(func):
    def wrapper():
        start = time.time()
        func()
        end = time.time()
        print(f"{ func.__name__ } executed in { '{:.2f}'.format(end - start) } seconds")

    return wrapper


@tictoc
def do_something_1():
    print("Doing something 1")
    time.sleep(2)
    print("Done something 1")


@tictoc
def do_something_2():
    print("Doing something 2")
    time.sleep(3.5)
    print("Done something 2")


do_something_1()
do_something_2()

  • Hash #: It is primarily used for comments to annotate code.


  • Dollar Sign $: Doesn't do anything in Python.


  • Modulo %: This is the modulo operator, the remain of division. It's also used to format strings.

x = 98

print(x%2, x%3, x%5, sep=" ___ ")
>> 0 ___ 2 ___ 3

print("x = %s" % x)
>> x = 98

  • Caret ^: Bitwise Exclusive Or (XOR).

a = 0b1111000010
b = 0b0000111110

x = a ^ b

print(bin(a), bin(b), bin(x), sep=" ___ ")
print(a, b, x, sep=" ___ ")

>> 0b1111000010 ___ 0b111110 ___ 0b1111111100
>> 962 ___ 62 ___ 1020

  • Ampersand &: Bitwise And operator.

a = 0b00110011
b = 0b11110000

x = a & b

print(bin(x))

>> 0b110000

  • Asterisk *: The uses are:

    • Multiplication operator

    print(4 * 5)
    print(3 * "Hello ")
    
    >> 20
    >> Hello Hello Hello
    • Unpacking operator

    a, b, *others = (4, 5, 12, 4, 5, -2)
    print("a = %s, b = %s, others = %s" % (a, b, others))
    
    >> a = 4, b = 5, others = [12, 4, 5, -2]
    • Allow any number of positional arguments: as a tuple

    def say_hello(*args):
        print(args)
    
    
    say_hello("Choaib", 22, "ENSA")
    >> ('Choaib', 22, 'ENSA')
    • Allow any number of keyword arguments: as a dictionary

    def say_hi(**kwargs):
        print(kwargs)
    
    
    say_hi(name="Choaib", age=22, school="ENSA")
    >> {'name': 'Choaib', 'age': 22, 'school': 'ENSA')
    • Import everything from a module

    from module_name import *

  • Brackets ( ): Mainly used in functions, create tuples and packing.


  • Curly Brackets { }: Mainly used to create dictionaries or sets, and in f-strings.


  • Square Brackets [ ]: Mainly used to create lists, access dictionary and tuple items.


  • Plus Sign +: The plus operator.


  • Minus Sign -: The minus operator.


  • Underscore _: The only character that can be inside a variable name.


  • Equal Sign =: Assignment operator. It can be combined with other punctuations to create combined assignment operators like: +=, -=, *=, /=, %= and ==.


  • Slash /: Mainly used for true division / and floor division //.


  • Backslash \: Mainly used as an escape character. Used also to split a long line of code into multiple lines.


  • Pipe Sign |: Bitwise Or operator.

a = 0b01010101
b = 0b10101010

x = a | b

print(bin(x))

>> 0b11111111

  • Semicolon ;: Used to squeeze multiple lines of code into one.


  • Colon :: Used in control structures (if else, loops, ...), function definition, try except blocks.


  • Single/Double Quotes ' ": Mainly used with strings.


  • Less/More than < >: Comparison operators, they can be combined with = to create <= and >=. They are also used as a left/right shifts operators.

a = 0b110011

print(bin(a))
print(a)

# Left Shift === Multiplication by 2
print(bin(a << 1))
print(a << 1)

# Right Shift === Division by 2
print(bin(a >> 1))
print(a >> 1)

  • Comma ,: Used to separate the arguments/parameters for a function. It separates the elements in lists, dictionaries, sets and tuples.


  • Full Stop .: Used to access either the methods or the attributes of an object.

Releases

No releases published

Packages

No packages published

Languages