Python - Basic
Python - Basic
capitalize() Converts the first character to upper isspace() Returns True if all characters in the
case string are whitespaces
casefold() Converts string into lower case istitle() Returns True if the string follows the
rules of a title
center() Returns a centered string
isupper() Returns True if all characters in the
count() Returns the number of times a string are upper case
specified value occurs in a string
join() Joins the elements of an iterable to
encode() Returns an encoded version of the the end of the string
string
ljust() Returns a left justified version of the
endswith() Returns true if the string ends with string
the specified value
lower() Converts a string into lower case
expandtabs() Sets the tab size of the string
lstrip() Returns a left trim version of the
find() Searches the string for a specified string
value and returns the position of
where it was found maketrans() Returns a translation table to be used
in translations
format() Formats specified values in a string
partition() Returns a tuple where the string is
format_map() Formats specified values in a string parted into three parts
index() Searches the string for a specified replace() Returns a string where a specified
3 value and returns the position of value is replaced with a specified
where it was found value
isalnum() Returns True if all characters in the rfind() Searches the string for a specified
Python - Remove List Items thislist = [100, 50, 65, 82, 23]
thislist = ["apple", "banana", thislist.sort()
"cherry","union","peach"] print(thislist)
thislist.remove("banana") def myfunc(n):
print(thislist) return abs(n - 50)
thislist.pop(1) thislist.sort(key = myfunc)
print(thislist) print(thislist)
del thislist[1]
print(thislist) thislist = ["banana", "Orange", "Kiwi", "cherry"]
thislist.pop() thislist.sort()
print(thislist) print(thislist)
5 thislist.clear() thislist.sort(key = str.lower)
print(thislist) print(thislist)
del thislist thislist.reverse()
print(thislist)
thislist = ["apple", "banana", "cherry"] reverse() Reverses the order of the list
mylist = thislist.copy()
print(mylist)
mylist = list(thislist) sort() Sorts the list
print(mylist)
['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry'] Python Tuples
Tuples are used to store multiple items in a single
Python - List Methods variable.
list1 = ["a", "b", "c"] A tuple is a collection which is ordered and
list2 = [1, 2, 3] unchangeable.
list3 = list1 + list2 Tuples are written with round brackets.
print(list3)
list3=list1 Tuples được sử dụng để lưu trữ nhiều mục trong một
for x in list2: biến duy nhất.
list3.append(x) Bộ tuple là một bộ sưu tập được sắp xếp theo thứ tự
print(list3) và không thể thay đổi .
['a', 'b', 'c', 1, 2, 3] Tuples được viết bằng dấu ngoặc tròn.
['a', 'b', 'c', 1, 2, 3] basic
thistuple = ("apple", "banana", "cherry")
List Methods print(thistuple)
Python has a set of built-in methods that you can use print(len(thistuple))
on lists. ('apple', 'banana', 'cherry')
3
Method Description
thistuple = ("apple",)#you have to add a comma
append() Adds an element at the end of the list print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
clear() Removes all the elements from the list print(type(thistuple))
extend() Add the elements of a list (or any Python - Access Tuple Items
def tri_recursion(k):
if(k > 0):
result = k + tri_recursion(k - 1)
print(result,end=' ')
else:
result = 0
return result
tri_recursion(6)
1 3 6 10 15 21
Python Arrays
Note: This page shows you how to use LISTS as
ARRAYS, however, to work with arrays in Python
you will have to import a library, like the NumPy
library.
cars = ["Ford", "Volvo", "BMW"]
cars[0] = "Toyota"
print(cars)
x = len(cars)
print(x)
for x in cars:
print(x,end=' ')
cars.append("Honda")
12 print('\n',cars)
cars.pop(3)
print(cars)
['Toyota', 'Volvo', 'BMW']