Python
Python
Print()
Print(a)
a.title()
print(a.title())
a.upper()
print(a.upper())
a.lower()
print(a.lower())
code a = ‘ada’
b = “lovelace”
c = f’{a} {b}’
print(c)
code a = ‘ada’
b = “lovelace”
c = f’{a} {b}’
d = f”Hello, {c.title()}!”
print(d)
“\t”
Code print(‘\tPython’)
Result Python
“\n”
Code print(“\nPython”)
Result
Python
a.rstrip()
code a = ‘Python ‘
print(a.rstrip())
result Python
a.lstrip()
code a = ‘ Python’
print(a.lstrip())
result Python
a.strip()
code a = ‘ Python ‘
b = “ Java”
print(a.strip())
print(b.strip())
result Python
Java
a.removeprefix(“b”)
code a = ‘box’
print(a.removeprefix(“b”))
result ox
a.removesuffix(“b”)
code a = ‘tub’
print(a.removesuffix(“b”)
result tu
a.append(‘C’)
print(a.append(‘C’))
result [A, B, C]
a.insert(2, ‘C’)
print(a.insert(2, ‘C’))
result [A, B, C, D]
del a[1]
del a[1]
print(a)
result [A, C]
a.sort()
a.sort()
print(a)
a.sort(reverse = True)
print(a)
sorted(a)
print(sorted(a))
a.reverse()
a.reverse()
print(a)
print(len(a))
result 3
Looping
For b in a:
Print(b)
Result A
Print(n)
Result 0
Min(a)
Print(min(a))
Result -1
Max(a)
Print(max(a))
Result 3
Sum(a)
Code a = [1, 2, 3, 4]
Print(sum(a))
Result 10
List Comprehension
Result e
Slicing a list
Print(A[1 : 3])
Chapter 1
Variable
Variable Modification
A variable can be modified without creating a new variable, by setting again. For example,
Code a = “a”
Print(a)
A = a.upper()
Print(a)
Result a
Code a = “a”
If a == “a”:
A=0
Print(a)
Result 0
Note
A variable should be created clearly for the Python interpreter to make it easy to read the variable
without mixing with the Python keyword or the integer.
A variable shouldn’t be thought as a box but as a label that is assigned to certain data(s).
String
A string carries data(s) that is only affected by the string related Python keyword(s) and is put in single or
double quote(s).
Number
Numbers are divided into integers which aren’t decimals and floats which are decimals. Both of them
can add by using +, substrate by using -, multiply by using *, divide by using /, power by ** and find
remainder by using %.
Note
Multiple Assignment
Variables can be assigned by using only one sentence in which each is separated by comma(s). For
example,
Code x, y, z = 0, 1, 2
Constant
A constant is written using capital letter(s) and used to describe that this variable will not change.
Chapter 2
List
List
A list contains more than one data in a particular order and these informations are separated by comma
and are put in square brackets.
In a list, the items are ordered, so that each item can be accessed by putting its position number in
square brackets after the variable of the list without spacing. For example,
Print(bicycles[0])
Print(bicycles[-1].title())
Result trek
Specialized
Print(motorcycles)
Motorcycles[0] = ‘ducati’
Print(motorcycles)
In a list, any new item can be inserted into any position of the list, by using insert(), a Python keyword,
putting the position number and then, placing a comma to set the new item after it, and the rest of the
list will be followed by the new item with their recently changed positions. For example,
Motorcycles.insert(0, ‘ducati’)
Print(motorcycles)
Note
The Python keyword, insert(), set the item to a certain position and that position can’t be changed that
when another item is also placed to the same position, that another item was moved to the right of it.
Appending a new item to the end of a list
In a list, by using append(), a Python keyword, any new item can be added(appended) to the end of the
list. For example,
Print(motorcycles)
Motorcycles.append(‘ducati’)
Print(motorcycles)
In a list, an item can be removed by putting its position in the square brackets after the list’s variable
which follows del, the Python keywords. For example,
Print(motorcycles)
Del motorcycles[0]
Print(motorcycles)
[‘honda’, ‘suzuki’]
In a list, to remove an item according to its position, the position is put into the square brackets and to
recall the recently removed item, a variable must be set. For example,
First_owned = motorcycles.pop(0)
In a list, the exact value(s) or the variable carrying it, is put into the round brackets of remove(), a Python
keyword, to remove the value(s),the information(s) carried by the item, and to recall the recently
removed information(s), a variable must be set. For example,
Print(motorcycles)
Too_expensive = ‘ducati’
Motorcycles.remove(too_expensive)
Print(motorcycles)
Note
The remove(), a Python keyword, is also used to remove the data(s) that is repeated throughout a list.
Order of a list
In a list, the original order can be restored in a definite order temporarily by placing the variable of the
list in the brackets of sorted(), a Python keyword.
In a list, the original order can be restored in a definite order permanently by using sort(), a Python
keyword, and to restore in the opposite order, an argument, reverse = True, is put in the round brackets.
Reversing a list
In a list, the order can be reversed by putting reverse(), a Python keyword, after the variable of the list.
Length of a list
In a list, to calculate the number of item(s), len(), a Python keyword, can be used by putting the variable
into the round brackets.
In a list, to refer each item, the variable which represents each of the items is put after a Python
keyword, for, and then, a Python keyword, in, follows the statement putting the variable of the list
behind it with a comma after the variable. Then, to perform the same task for each referred item, every
code for the task must be written after a tab at least. For example,
Print(magician)
Result alice
David
Carolina
To loop a numerical sequence, a Python function, range(), is used by putting starting number(not
necessary) which is 0 at default and known as start, by putting stopping number(necessary) which is
known as stop, and by putting stepping number which defines how many steps are taken for the count ,
is 1 at default and known as step. For example,
Print(value)
Result 0
4
Note
By using range(), a Python function, an odd numerical sequence, an even numerical list or some
numerical lists can be created as the creator’s imagination.
A numerical sequence can be built by creating the variable which represents the list and directly
modifying the variable which represents the item. For example,
Print(value)
To create a numerical list, the numerical sequence formed by range(),a Python keyword, is created in the
round brackets of a python keyword, list(). For example,
Print(numbers)
Result [1, 2, 3, 4, 5]
Slicing a list
To work with a specific segment of the list, an arrangement in which the variable of the list is followed by
the first number, the symbol of ratio, and the second number inside the squared brackets. The first
number is the starting item and the second number is the ending item. In addition, if there is only a first
number which has a negative sign, the list will be counted backward and that first number will be the
number of items in the resultant list. For example,
Print(A[1 : 3])
Print(A[-1 : ])
Result [d]
Print(A[ : -2])
Print(A[ : ])
Note
The default of the number of included item is the same as the number of all items in the list.
The negative number cannot be used if both of the first number and the second number exist at the
same time.
For i in A[ : 3]:
Print(i)
Result a