본문 바로가기

Python

Python_G-004 : 10 basic operations & functions

 

10 Basic Operations

Basic math operations 2 + 3,
5 - 1,
4 * 2,
8 / 2,
5 % 2   # output 1
 
Assign values to variables a = 5,
b += 3    # b = b+3
c *= 2     # c = c*2
 
Compare values a == b    # equal
a != b     # not equal
a > b
a < b
 
Logical operations a and b
a or b
not a
 
Bit-level operations a & b, `a b,a ^ b,~a,a << b,a >> b`
Test for membership in sequences 'a' in 'abc', 3 not in [1, 2, 4]  
Compare object identities a is b, a is not b  
Operations on lists len(list), list.append(4), list[0]  
Operations on strings str1 + str2, str1 * 3, len(str1)  
Operations on dictionaries dict.keys(), dict.values(), dict['key']  
 

10 Basic Functions

print() Prints the specified message to the console print("Hello, world!")
len() Returns the length (number of items) of an object len([1, 2, 3])         # Output: 3
type() Returns the type of an object type(42)           # Output: <class 'int'>
int() Converts a value to an integer int(3.14)           # Output: 3
str() Converts a value to a string str(42)           # Output: "42"
list() Converts a value to a list list((1, 2, 3))            # Output: [1, 2, 3]
sum() Returns the sum of all items in an iterable sum([1, 2, 3, 4])           # Output: 10
max() Returns the largest item in an iterable max([1, 2, 3, 4])           # Output: 4
min() Returns the smallest item in an iterable min([1, 2, 3, 4])           # Output: 1
range() Generates a sequence of numbers
 range( n ) : 0 to (n-1)
 range( x, y ) : x to (y-1)
for i in range(5): print(i)       # Output: 0, 1, 2, 3, 4

 

'Python' 카테고리의 다른 글

Python_G-007. List vs Tuple vs set vs dictionary  (0) 2025.01.16
Python_G-006. 10 Basic Plot (with Matplotlib)  (0) 2025.01.12
Python_G-005. Loop Type  (0) 2025.01.12
Python_G-003 : Variable Type  (0) 2025.01.12
Python_G-002 : 10 basic Libraries  (0) 2025.01.12