Quick actions

cmd+k|ctrl+k

Navigation

Languages

Foundation

Snippet info

Language

Python

Visibility

public

Author

shahzor30

Created

2024-10-30T01:52:24.258636Z

Updated

2024-10-30T02:49:52.610323Z

# variable declaration
# ============================
some_variable = None;
some_variable = 5;
x,y,z = 10,20,30;       # x=10; y=20; z=30

# data types
type(10);                # int
type(10.55);             # float
type('Hello World');     # str
type(False);             # bool
type([]);                # list
type(None);              # NoneType


# data type conversion
# ============================
str(10);                 # int to str
int('100');              # str to int
float(10)                # int to float
float('100');            # str to float
bool(1);                 # int to bool
bool('True');            # str to bool


# string data type operations
# ============================
'*' * 10;                # **********
'ali'.upper();           # ALI
'ali'.capitalize();      # Ali
'ALI'.lower();           # ali
'to be or not to be'.replace('be', 'do');    # to do or not to do
len('ali');              # 3
','.join(['a','b']);     # a,b

name = 'Ali'
print(f'Name: {name}');          # Name: Ali


# number data type (int, float) operations
# ========================================
2 ** 3;                 # 8
5 / 4;                  # 1.25
5 // 4;                 # 1 (rounded down to int)
6 % 4;                  # 2 (remainder)
abs(-3.4);              # 3.4 (positive)
bin(5);                 # 0b101 (Binary representation... 101)
round(3.5);             # 4

# List data type operations
# ============================
some_list = [0,1,2,3,4,5,6,7,8,9];
len(some_list);     # 10

# [start:stop:stepover]
some_list[0];        # 0
some_list[:];        # 0123456789
some_list[0:];       # 0123456789
some_list[0:8];      # 01234567
some_list[2:5];      # 234
some_list[:4];       # 0123
some_list[0:8:2];    # 024
some_list[::2];      # 0246
some_list[-1];       # 9
some_list[::-1];     # 9876543210

some_list[0] = -1;  # list are mutable

# Note: string is also a list of characters i.e. '0123456789'[0] => 0
# strings are imutable i.e. '0123456789'[0] = -1 will throw error

some_list_copy = some_list[:];          # list copy or clone
some_list_copy2 = some_list.copy();      # list copy or clone

grades = ['A','B'];
grades.append('C');         # ['A','B','C']
grades.insert(1,'A+')       # ['A','A+',B','C']
grades.extend(['D','F']);   # ['A','A+',B','C','D','F']

grades.pop();               # ['A','A+',B','C','D']
grades.pop(1);              # ['A',B','C','D']
grades.remove('B');         # ['A', 'C', 'D']

grades.index('A');           # 0
'A' in grades;              # True

grades.append('A');         # ['A', 'C', 'D', 'A']
grades.count('A');          # 2 i.e. A comes 2 times

['X', 'Z', 'Y'].sort();         # ['X','Y','Z'] modifies the original list
sorted(['X', 'Z', 'Y']);        # ['X','Y','Z'] returns a new sorted list
['X', 'Z', 'Y'].reverse();      # ['Y','Z','X']
['X', 'Z', 'Y'][::-1];          # ['Y','Z','X']

grades.clear();                 # []

marks = list(range(1, 5));      # [1,2,3,4,5]
marks = list(range(5));         # [0,1,2,3,4]


# Lists usage as Matrix
# =======================
matrix = [[1,2,3],[4,5,6],[7,8,9]]
print(matrix[0][0] + matrix[1][1] + matrix[2][2]);    # 1+5+9 => 15

# List unpacking
a,b,c,*other, d = [1,2,3,4,5,6,7];     # a=1; b=2; c=3; other=[4, 5, 6]; d=7






INFO