Python-Basic
2024. 11. 27. 01:28ㆍComputational biology
Python has 6 data types
- Numeric Types: int (integer), float (decimal), complex (complex number)
- Sequence Types: str(string), list(list), tuple(tuple)
- Mapping Type: dict
- Set Types: set
- Boolean Type: bool (Boolean)
- Binary Types: bytes, bytearray, memoryview
✍🏻 python# 데이터 타입 v_str1 = "Niceman" #str v_str2 = "Goodgirl" #str v_bool = True #bool v_float = 10.3 #float v_int = 7 #int v_complex = 3 + 3j v_dict = { "name" : "Jang", "age" : 32, "city" : "seoul" } #dict v_list = [3,5,7] #list v_tuple = 3,5,7 #tuple v_set = {7,8,9} #set # print(type(v_str1)) # <class 'str'> print(type(v_str2)) # <class 'str'> print(type(v_bool)) # <class 'bool'> print(type(v_float)) # <class 'float'> print(type(v_int)) # <class 'int'> print(type(v_complex)) # <class 'complex'> print(type(v_dict)) # <class 'dict'> print(type(v_list)) # <class 'list'> print(type(v_tuple)) # <class 'tuple'> print(type(v_set)) # <class 'set'>
1. Numeric Types : Int / Float / Complex
- Int (Integer): A type of numerical value, an integer without a decimal point.
- Float (decimal): A type of numerical value that is a real number with a decimal point.
- Complex: A complex number that is the sum of real and imaginary numbers.
- Generally, all possible functions in mathematics can be used.
- Mathematical functions can be expanded and utilized through import math.
✍🏻 python# Arithmetic operations # Addition(+), subtraction(-), multiplication(*), division(/), quotient( //), remainder(%), square(**) a = 10 b = 2 print (a+b) # 12 print (ab) # 8 print (a*b) # 20 print (a/b) # 5.0 print (a//b) # 5 print (a%b) # 0 print (a**b) # 100
#단항 연산자 x = 100 x += 100 # x = x + x print(x) #200 y = 100 y /= y # y = y / y print(y) #1.0 z = 7 z *= z # z = z * z print(z) # 49 m = 99999999 m −= m # m = m − m print(m) # 0
# Numerical operation function print ( abs ( -7 )) # Absolute value: 7 n, m = divmod ( 100 , 8 ) print (n, m) # 12 4
import math print (math. ceil ( 5.1 )) # Round up: 6 print (math. floor ( 3.874 )) # Round down: 3
2. Sequence Types : Str / List / Tuple
1) Character type (str)
- A string is a collection of characters, such as letters or words.
✍🏻 python# Creating a string 1: Surrounding both sides with double quotes (") print ( "Hello world!" ) # Hello python!
# Creating a string 2: Surrounding both sides with single quotes (') print ( 'Hello world!' ) # Hello python!
# Creating a string 3: Surrounding both sides with three consecutive double quotation marks (""") print( """Hello world!""" ) # Hello python!
# Creating a string 4: Three single quotes in a row (''') print( '''Hello world!''' ) # Hello python!
# When you want to include ' in a string: enclose it in double quotes ("") print( "Python's favorite food is pizza" ) # Python 's favorite food is pizza
# When you want to include " in a string: enclose it in single quotes ('') print ( '"Hi, Dave" he says.' ) # "Hi, Dave" he says.
# When you want to include double quotes(") or single quotes(') in a string: \(backslash) on the left side food = 'Python\'s favorite food is perl' print (food) # Python's favorite food is perl say = "\"Python is very easy.\" he says." print ( say ) # "Python is very easy." he says.
# Insert escape code(\n) for line break multiline = "Life is too short\nYou need python." print(multiline) # Life is too short # You need python.
- The character type provides various add, insert, modify, and delete functions to handle characters.
- Finding length, indexing, slicing, counting, finding positions, inserting, replacing, dividing, etc.
✍🏻 python# Find the length of a string: len() getlength = 'python is cool' print (len(getlength)) # 14
# String indexing indexing = "Life is too short, You need Python" print(indexing[ 3 ]) # e -> Starting from 0 , 3 is the 4th position print(indexing[ -1 ]) # n -> -1 is the 1st position starting from the back
# String slicing slicing = "Life is too short, You need Python" print (slicing[ 0 : 4 ]) # Life -> [x:y] Extract the string from x to y (excluding the last number) print (slicing[:]) # Life is too short, You need Python print (slicing[: 4 ]) # Life print (slicing[ 18 :]) # You need Python print (slicing[ -1 : 7 ]) # Python
# Count the number of characters (count) count_str = "lalalalalballlballballballllblblblblaaaa" print(count_str.count( "l" )) # 20 -> Get the number of l in count_str
# Find the location (find) find_str = "python!" print (find_str. find ( "!" )) # 6 -> ! is in the 6th position print (find_str. find ( "a" )) # -1 -> a is not in find_str
# 문자열 삽입(join) join_str = "python" join_str = "python" print(".".join(join_str)) # p.y.t.h.o.n print("-".join(join_str)) # p-y-t-h-o-n
# Convert lowercase letters to uppercase (upper) a = "hi" print( a . upper ()) # HI
# Convert uppercase to lowercase (lower) #python a = "PYTHON" print( a . lower ())
# Remove whitespace: Remove whitespace on both sides (strip) / Remove whitespace on the left (lstrip) / Remove whitespace on the right (rstrip) a = " hi " print( a .strip()) # hi
# 문자열 바꾸기(replace) a = "Life is too short" print(a.replace("short", "long")) # Life is too long
# Splitting a string ( split ): Divide the string based on the string in () and insert the divided values into a list a = "Life is too short" print (a. split ()) # [ 'Life' , 'is' , 'too' , 'short' ] b = "a:b:c:d" print (b. split ( ":" )) # [ 'a' , 'b' , 'c' , 'd' ]
2) list
- You can use lists to group and use data.
- Improves the problem of having to create each variable unnecessarily and store data one by one
- Lists can be indexed and sliced like strings.
- Also, you can edit, delete, add, sort, flip, insert, etc.
✍🏻 python# indexing: Extracting desired elements by their position in the list indexing = [ 1 , 2 , 3 , 4 , 5 ] print(indexing) # [ 1 , 2 , 3 , 4 , 5 ] print(indexing[ 0 ]) # 1 print(indexing[ 0 ]+indexing[ -1 ]) # 6 => 1 + 5 indexing = [ 1 , 2 , 3 , [ "a" , "b" , "c" ]] print(indexing[ 0 ]) # 1 print(indexing[ 3 ]) # [ "a" , "b" , "c" ] print(indexing[ 3 ][ -1 ]) # c
# slicing: Get the desired value by position in the list slicing = [ 1 , 2 , 3 , [ "a" , "b" , "c" ], 4 , 5 ] print(slicing[ 2 : 5 ]) # [ 3 , [ 'a' , 'b' , 'c' ], 4 ] -> Get from the second to the fifth (excluding the last number)
# Modify list values a = [ 1 , 2 , 3 ] a[ 2 ] = 4 # Change the 2nd digit to 4 print ( a) # [ 1 , 2 , 4 ]
# Delete list values (del) a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] del a[ 9 ] # Delete the 9th digit print(a) # [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 9 ] del a[ 3 :] # Delete everything after the 3rd digit print(a) # [ 1 , 2 , 3 ]
# Add list values (append, extend) a = [ 1 , 2 , 3 , 4 , 5 ] a.append( 6 ) # Insert 6 as an element in list a print(a) # [ 1 , 2 , 3 , 4 , 5 , 6 ] a.append([ 7 , 8 , 9 , 10 ]) # Insert the list [ 7 , 8 , 9 , 10 ] as an inner list in list a print(a) # [ 1 , 2 , 3 , 4 , 5 , 6 , [ 7 , 8 , 9 , 10 ]] b = [ 1 , 2 , 3 , 4 , 5 ] b.extend([ "a" , "b" , "c" , "d" ]) # Append is Adding an array within an array, but extending it print(b) # [ 1 , 2 , 3 , 4 , 5 , 'a', 'b', 'c', 'd']
# Sort the list a = [ 1 , 4 , 3 , 2 , 5 ] a.sort() # Sort in ascending order print(a) # [ 1 , 2 , 3 , 4 , 5 ]
# Reverse the list ( reverse ) a = [ 'a' , 'b' , 'c' , 'z' , 'f' , 'i' ] a. reverse () print (a) # [ 'i' , 'f' , 'z' , 'c' , 'b' , 'a' ]
# Insert list elements a = [ 1 , 2 , 3 ] a.insert( 0 , 10 ) # Insert 10 in the 0th position print (a) # [ 10 , 1 , 2 , 3 ]
# Remove list elements: Delete the first element a = [ 1 , 2 , 3 , 1 , 2 , 3 ] a.remove( 3 ) # Delete the first 3 in the list print(a) # [ 1 , 2 , 1 , 2 , 3 ]
# Popping out a list element - Popping out the last element and deleting it from the existing list a = [ 1 , 2 , 3 ] print (a) # [1,2,3] a.pop() # Popping out one element (3) from the end print (a) # [1,2] a.pop() # Popping out one element (2) from the end print (a) # [1] a.pop() # Popping out one element (1) from the end print (a) # []
# Count the number of elements x in the list (count) a = [ 1 , 2 , 3 , 1 ] b = [ "a" , "b" , "c" , "a" , "d" , "e" , "a" ] print(a.count( 1 )) # 2 -> There are 2 instances of 1 in list a print(b.count( "a" )) # 3 -> There are 3 instances of a in list b
3) Tuple
- Tuples are similar to lists in general, except for two things:
- Difference 1. Lists are wrapped in [], but tuples are wrapped in ().
- Difference 2. Lists allow you to create, delete, and modify elements, but tuples cannot change their values.
- However, tuples can perform basic operations such as indexing, slicing, addition, multiplication, and finding the length.
- Addition is possible between tuples, but multiplication is only possible by multiplying a tuple by a number (tuples cannot be multiplied).
- Also, if a tuple has one value, you must put a comma (,) after the value, and () can be omitted.
✍🏻 python# 튜플 기본 tuple1 = () tuple2 = ( 1 ,) tuple3 = ( 1 , 2 , 3 ) tuple4 = 1 , 2 , 3 tuple5 = ( "a" , "b" , ( "ab" , "cd" , ( "xyz" ,)))
print(tuple1) # () print(tuple2) # (1,) print(tuple3) # (1, 2, 3) print(tuple4) # (1, 2, 3) print(tuple5) # ('a', 'b', ('ab', 'cd', ('xyz',)))
# Working with tuples # Indexing tuples tuple5 = ( "a" , "b" , ( "ab" , "cd" , ( "xyz" ,))) print (tuple5[2]) # ( 'ab' , 'cd' , ( 'xyz' ,))
# 튜플 슬라이싱하기 tuple5 = ("a", "b", ("ab", "cd", ("xyz",))) print(tuple5[1:]) # ('b', ('ab', 'cd', ('xyz',)))
# Add tuples tuple3 = ( 1 , 2 , 3 ) tuple4 = 1 , 2 , 3 print(tuple3 + tuple4) # ( 1 , 2 , 3 , 1 , 2 , 3 )
# Multiply tuples tuple3 = ( 1 , 2 , 3 ) print(tuple3 * 3 ) # ( 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 )
# 튜플 길이 구하기(len) tuple5 = ("a", "b", ("ab", "cd", ("xyz",))) print(len(tuple5)) # 3 -> ("ab", "cd", ("xyz",) print(len(tuple5[2][2])) # 1 -> ("xyz",)
# Utilizing tuple values: When swapping variable values! # In other languages, to swap the values of two variables, you have to create a temporary variable temp, move it, and then put it back. # Tuples can be easily done with x , y = y , x x = 3 y = 9 x , y = y , x print( x ) # 9 print( y ) # 33
3. Mapping Type : dict
1) Dictionary
- Similar to an array in JavaScript, it has keys and values.
- Obtains values through key values without requiring sequential index values like a list.
- {} You can create it by wrapping it in curly brackets. {key1:value1, key2:value2, key3:value3, ...key(N):value(N)}
- Add elements, delete elements, get values, clear, etc...
✍🏻 python# 딕셔너리 기본 형태 dict1 = {"name":"jawon", "age":20, "phone":"010-1111-2222"} dict2 = {1:"hi"} dict3 = {"a":[1,2,3]} dict4 = {}
# Adding a dictionary pair: Similar to adding a list, but put the key value in [] dict1 = { 1 : 'a' } dict1[ 2 ] = "b" # Add print(dict1) # {1: 'a' , 2: 'b' } dict1[ "name" ] = "jaewon" # Add dict1[ "age" ] = 20 # Add print(dict1) # {1: 'a' , 2: 'b' , 'name' : 'jaewon' , 'age' : 20} dict1[ 3 ] = [ 1 , 2 , 3 , 4 ] print(dict1) # {1: 'a' , 2: 'b' , 'name' : 'jaewon' , 'age' : 20, 3: [1, 2, 3, 4]}
# Deleting dictionary elements (del): You can delete them with del dictName[key value], and the key:value pairs are deleted. dict2 = { "name" : "jaewon" , "age" : "20" , "location" : "seoul" , 1: "a" , "list_a" :[1,2,3,4,5]} del dict2[ "list_a" ] print (dict2) # { 'name' : 'jaewon' , 'age' : '20' , 'location' : 'seoul' , 1: 'a' } del dict2[1] print (dict2) # { 'name' : 'jaewon' , 'age' : '20' , 'location' : 'seoul' }
# Getting a value from a dictionary # When you want to get an element value from a list, tuple, or string, you use indexing or slicing techniques, # but in a dictionary, you use a method to get a value using a key # Because there is no index order in a dictionary, and indexing is done by key a = {1: "a" , 2: "b" , "name" : "jaewon" , "age" :20, "a" :[1,2,3,4,5]} print (a[1]) # a print (a[ "name" ]) # jaewon print (a[ "age" ]) # 20 print (a[ "a" ]) # [1,2,3,4,5]
# Get value through get -> You can call value through key as above, but if you call a key that does not exist, an error will occur # When getting value through get, if you use a non-existent key, None will be returned print ( "---------------" ) a = { 'name' : 'pey' , 'phone' : '0119993323' , 'birth' : '1118' } print (a[ "name" ]) # pey print (a. get ( "name" )) # pey # print(a["location"]) # Error occurs print (a. get ( "location" )) # None is returned print (a. get ( "location" , "value is not" )) # value is not returned -> You can set a default value in advance the second time and call it when it does not exist.
# Extract key values sequentially c = {'name': 'pey', 'phone': ' 0119993323 ', 'birth': ' 1118 '} for k in c.keys(): print(k) # name, phone, birth -> You can retrieve key values one by one
# Create a list by extracting key values: dictName.key ( ) list_c_keys = list (c.keys()) # After extracting key values, wrap them in a list p rint( list_c_keys) # [ 'name' , 'phone' , 'birth' ]
# Create a list by extracting values: dictName. values () list_c_values = list ( c . values ()) print (list_c_values) # [ 'pey' , '0119993323' , '1118' ]
# Insert key, value pairs into a list: dictName. items () list_c_items = list ( c . items ()) print (list_c_items) # [( 'name' , 'pey' ), ( 'phone' , '0119993323' ), ( 'birth' , '1118' )]
# key와 value값 모두 지우기 : dictName.clear() d = {"name":"jaewon", "age":20, "location":"seoul", "lulu":[1,2,3,4,5]} d.clear() print(d) # {}
# Check if the key is in the dictionary ( in ) -> Returns True if it is , False if it is not a = { 'name' : 'pey' , 'phone' : '0119993323' , 'birth' : '1118' } print( "name" in a) # True print( "birth" in a) # True print( "location" in a) # False
4. Set Type : set
1) set
- A set data type can be created using the set keyword. Empty set: set()
- Since sets do not allow duplicates, when converted to a set, duplicate values are removed (often used as a filter function to remove duplicates)
- Because sets are unordered, stored values cannot be accessed by indexing.
- In order to store values stored by indexing in a set, the data type must be converted to a list or tuple and then controlled.
- Sets are used to find intersection, union, and difference.
✍🏻 python# Basic form of set: Can be created in list or string form s1 = set ([1,2,3,1,2,3]) s2 = set ( "Hello" ) print (s1) # {1,2,3} print (s2) # { 'l' , 'o' , 'e' , 'H' } -> Duplicates are removed
# Since indexing is not possible in a set, convert it to a list or tuple and then control it to extract the element value list_s1 = list (s1) # Convert list list_s2 = list (s2) # Convert list print (list_s1) # [1, 2, 3] print (list_s2) # ['e', 'o', 'H', 'l'] print (list_s1[ 1 ]) # 2 print (list_s2[ 1 ]) # H -> The value keeps changing,, The order is randomly located in the process of converting to a list.
# Finding intersection, union, and difference s1 = set ([ 1 , 2 , 3 , 4 , 5 , 6 ]) s2 = set ([ 4 , 5 , 6 , 7 , 8 , 9 ])
# Intersection: Intersection extracts common elements between sets print(s1&s2) # { 4 , 5 , 6 } print(s1. int ersection(s2)) # { 4 , 5 , 6 }
# Union: Merge all elements between sets without duplicates print(s1|s2) # { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } print(s1.union(s2)) # { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }
# Set difference: Subtraction between sets print (s1-s2) # {1, 2, 3} print (s1. difference (s2)) # {1, 2, 3}
# Set-related functions # Adding 1 value (add): Only 1 is possible s1 = set ([ 1 , 2 , 3 ]) s1.add( 4 ) print(s1) # { 1 , 2 , 3 , 4 }
# Add multiple values (update) s1 = set ([ 1 , 2 , 3 ]) s1.update([ 4 , 5 , 6 ]) print(s1) # { 1 , 2 , 3 , 4 , 5 , 6 }
# remove specific values s1 = set ([1,2,3]) s1. remove (3) print (s1) # {1, 2}
# set usage: no duplication, set operation data1 = { "apple" , "samsung" , "lg" } data2 = { "samsung" , "lg" , "MS" } print(data1&data2) # { 'lg' , 'samsung' } -> intersection print(data1|data2) # { 'lg' , 'MS' , 'apple' , 'samsung' } -> union print(data1-data2) # { 'apple' } -> difference print(data1^data2) # { 'MS' , 'apple' } -> union-intersection
#중복 없애기 data_list = ["apple", "samsung", "lg", "samsung", "lg", "MS", "dell", "HP", "dell", "HP", "samsung"] # {'dell', 'samsung', 'MS', 'lg', 'HP', 'apple'}
5. Boolean Type : bool
- The Boolean data type is a data type that represents True and False. It can only have these two values.
- Empty data values ("", (), {}, [], etc.), number 0, None are False, the rest are True
- Used with conditional statements
Type change
- Type conversion can be done using the type() function.
✍🏻 pythonint1 = 10 int2 = 200 float1 = .5 float2 = 10. #형변환 int(정수), float(실수), complex(복소수) print(int(float1)) #0 print(int(float2)) #10 print(float(int1)) #10.0 print(complex(3)) #(3+0j) print(int(True)) #1 print(int(False)) #0 print(type(int(True))) #class 'int' print(type(int(False))) #class 'int' print(int('7')) # 7 print(complex(False)) #0j
From Jjangjae's blog
'Computational biology' 카테고리의 다른 글
Bio Python example code (0) | 2024.11.27 |
---|---|
biopython (0) | 2024.11.27 |
python libraries (1) | 2024.11.27 |
scRNA-seq Raw Data Preprocessing: scRNA-seq quality control (0) | 2024.11.27 |
R packages (0) | 2024.11.27 |