Ilham Tech Python Cheat Sheet: 1. Basic
Ilham Tech Python Cheat Sheet: 1. Basic
1. Basic
Print
In [1]: print("Hello World!")
Hello World!
Input
In [2]: nama = input("siapa nama anda?")
Comment
In [3]: # ini comment
Operation
In [4]: 1 + 1
Out[4]: 2
In [5]: 1/2
Out[5]: 0.5
In [6]: 5-2
Out[6]: 3
In [7]: 5*2
Out[7]: 10
In [8]: 5**2
Out[8]: 25
In [10]: 5**3
Out[10]: 125
Variables
In [12]: x = 1+2
print(x)
In [13]: x = 3
x += 2
print(x)
2. Data Type
Integer
In [14]: data_type_1 = 10
print(type(data_type_1))
<class 'int'>
Float
In [15]: data_type_2 = 10.52
print(type(data_type_2))
<class 'float'>
String
In [16]: data_type_3 = "ilham tech"
print(type(data_type_3))
<class 'str'>
Hello world!
This is ILHAM TECH
In [21]: # string concatenation
In [22]: #f-string
variable = "ILHAM"
<class 'float'>
In [26]: x = 10
print(type(str(x)))
<class 'str'>
3. Functions
In [27]: def my_function1(x):
y = x * 3
return y
In [28]: my_function1(9)
Out[28]: 27
In [33]: my_function2("elyas","28")
In [32]: my_function3()
Hello World!
4. Conditions
In [34]: x = 4
if x > 2:
print("x is greater than 2")
x is greater than 2
In [37]: y = 20
if y < 9:
print("y is less than 9")
elif y < 15:
print("y is less than 15")
else:
print("y is greater than or equal to 15")
In [38]: a = 50
statement 1 is correct
In [40]: a = 30
statement 1 is correct
5. Loops
For Loop
In [41]: list_fruits = ["apple","orange","durian","banana","grape","lemon"]
apple
orange
durian
banana
grape
lemon
In [45]: n = 5
for i in range(5):
#print(i)
n += i
print(n)
5
6
8
11
15
While Loop
In [46]: n = 5
while n>0:
n = n - 1
print(n)
4
3
2
1
0