Syntax
- Datatype
- if statement
- for loop
- class
Data type
int
foo = 1
bar = 2
print(foo + bar)
print(foo - bar)
print(foo / bar)
print(foo * bar)
print(foo % bar)
print(type(foo)) # <class 'int'>
float
baz = 1.5
print(type(baz)) # <class 'float'>
str
foo = "kei"
bar = "oka"
print(foo + bar)
# "keioka"
You can take range out of string
foo = "kei"
print(foo[1:2])
Built-In
lower() | |
---|---|
upper() | |
count() | |
find() | |
split() | |
index() | |
join() | |
something.. | |
something... | |
sonething... |
#lower() and #upper() ( #capitalize() )
foo = "kei"
bar = "Oka"
print(foo.upper()) # "Kei"
print(foo.lower()) # "Oka"
#count()
foo = "kei"
print(foo.count("k"))
#find()
foo = "kei"
print(foo.find("k"))
# => 0 return index
#split()
Unlike other languages, you can not split string with empty string ("") which will generate array of the string
foo = "kei"
print(foo.split(""))
# => Error
Instead
foo = "kei"
list(foo)
# => ["k", "e", "i"]
#index()
This method returns the start index of substring or throwsValueError
when it does not find the requested item
foo = "Kei"
sentence = "hello, I am Kei"
print(sentence.index(foo))
# => 12
#join()
foo = "!"
x = foo.join(["hello", "I am", "Kei"])
print(x)
# => hello!I am!Kei
list
list1 = [1, 2, 3, 4, 5]
Concating lists
a = [1, 2, 3]
b = [4, 5, 6]
x = a + b
x
[1, 2, 3, 4, 5, 6]
a = [1, 2, 3]
b = [4, 5, 6]
a += b
a
// => [1, 2, 3, 4, 5, 6]
You can take range out of list.
list1[2:5]
Delete the range of elements on a list.
foo = [1, 2, 3, 4, 5, 6, 7, 8, 9]
foo[2:5] = []
print(foo) // [1, 2, 6, 7, 8, 9]
copy list
i = [1, 2, 3]
j = i
i[0] = 100
print(i) # => [100, 1, 2, 3]
print(j) # => [100, 1, 2, 3]
This is pass by reference. To create copy of list is ...
i = [1, 2, 3]
j = i.copy()
i[0] = 100
print(i) # => [100, 1, 2, 3]
print(j) # => [1, 2, 3]
or...
i = [1, 2, 3]
j = i[:]
i[0] = 100
print(i) # => [100, 1, 2, 3]
print(j) # => [1, 2, 3](j)
Built-in methods
append() | |
---|---|
pop() | |
remove() | The remove() method searches for the given element in the list and removes the first matching element. |
insert() | |
extend() | |
index() |
#append()
foo = [1, 2, 3]
foo.append(4)
//=> [1, 2, 3, 4]
#pop()
foo = [1, 2, 3]
foo.pop()
// => [1, 2]
#remove()
The remove() method searches for the given element in the list and removes the first matching element.
foo = [1, 3, 2, 4, 2]
foo.remove(2)
// => [1, 3, 4, 2]
#insert()
foo = [1, 2, 3]
foo.insert(0, 0)
// => [0, 1, 2, 3]
#extend()
foo = [1, 2, 3]
bar = [4, 5, 6]
foo.extend(bar)
print(foo)
#index()
Returns the index of the first matching element. We can pass number to second argument.
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
r.index(3)
print(r.index(3))
# => 2
print(r.index(3, 3))
# => 3
#count()
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
print(r.count(3))
# => 3
#if .. in
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
if 1 in r:
print('exist')
# => exist
#sort
sort list and also able to reverse when you pass reverse=True
as an argument.
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
r.sort()
print(r)
# => [1, 1, 2, 2, 3, 3, 3, 4, 4, 5]
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
r.sort()
r.sort(reverse=True)
print(r)
# => [5, 4, 4, 3, 3, 3, 2, 2, 1, 1]
#reverse
r = [1, 2, 3, 4, 5, 1, 2, 3, 4, 3]
r.reverse()
print(r)
# => [3, 4, 3, 2, 1, 5, 4, 3, 2, 1]
dict
taple
class User:
def __init__(self, name):
self.__name = name
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name;
tom = User("Kei")
tom.get_name()
tom.set_name("Tom")
# print(tom.get_name())
# print(tom._User__name) # can access
# list
n = 0
m = 4
l = [1, 20, 4, 50]
print(l[2:2])
# Set
c = {1, 2, 3, 4, 5}
print(type(c)) # <class 'set'>
# Dictionary
b = { "k": "kei" }
print(b["k"])
print(type(b)) # <class 'dict'>
print(b.items())
d = [('j', 'hel'), ('h', 'uyt')]
for key, value in b.items():
print(key, value)
for key, value in d:
print(key, value)
score = [40, 50, 60, 70]
# Iterator
it = iter(score)
print(next(it))
print(next(it))
print(next(it))
print(next(it))
print(type(it)) # <class 'list_iterator'>
# Generator
def get_data():
i = 0
while True:
yield i*2
i += 1
it = get_data()
print(type(get_data)) # <class 'function'>
print(next(it))
print(type(it)) # <class 'generator'>
print(next(it))
print(next(it))
#
def hello():
a = 0
# print(type(hello()))