0% found this document useful (0 votes)
57 views

Ilham Tech Python Cheat Sheet: 1. Basic

The document is a Python cheat sheet that provides examples of basic Python concepts like printing, variables, data types, functions, conditions, and loops. It shows the syntax for printing, taking user input, performing calculations and operations, defining variables and functions, using if/else statements to check conditions, and using for and while loops to iterate. The cheat sheet is intended to demonstrate fundamental Python programming concepts through code examples.

Uploaded by

sabar5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Ilham Tech Python Cheat Sheet: 1. Basic

The document is a Python cheat sheet that provides examples of basic Python concepts like printing, variables, data types, functions, conditions, and loops. It shows the syntax for printing, taking user input, performing calculations and operations, defining variables and functions, using if/else statements to check conditions, and using for and while loops to iterate. The cheat sheet is intended to demonstrate fundamental Python programming concepts through code examples.

Uploaded by

sabar5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Ilham Tech Python Cheat Sheet

1. Basic
Print
In [1]: print("Hello World!")

Hello World!

Input
In [2]: nama = input("siapa nama anda?")

siapa nama anda?elyas

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'>

In [18]: # double quote

print('Hello world! This is "ILHAM TECH"')

Hello world! This is "ILHAM TECH"

In [20]: # new line

print("Hello world! \nThis is ILHAM TECH")

Hello world!
This is ILHAM TECH
In [21]: # string concatenation

print("Hello World"+" THIS is ILHAM TECH")

Hello World THIS is ILHAM TECH

In [22]: #f-string

variable = "ILHAM"

print(f"This is {variable} TECH")

This is ILHAM TECH

Data Type Conversion


In [25]: x = 10
print(type(float(x)))

<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 [29]: def my_function2(nama, umur):


print(f"nama saya {nama}, umur saya {umur} tahun")

In [33]: my_function2("elyas","28")

nama saya elyas, umur saya 28 tahun

In [31]: def my_function3():


print("Hello World!")

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")

y is greater than or equal to 15

In [38]: a = 50

if a > 45 and a < 60:


print("statement 1 is correct")
else:
print("statement 1 is incorrect")

statement 1 is correct

In [40]: a = 30

if a > 45 or a < 60:


print("statement 1 is correct")
else:
print("statement 1 is incorrect")

statement 1 is correct

5. Loops
For Loop
In [41]: list_fruits = ["apple","orange","durian","banana","grape","lemon"]

for fruit in list_fruits:


print(fruit)

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

You might also like