Skip to content

Latest commit

 

History

History
187 lines (154 loc) · 3.15 KB

File metadata and controls

187 lines (154 loc) · 3.15 KB

Basic-python-course

Week 1

How to print to standard output

print('what to print')
  • output :
    what to print

How to write comments

Single Line

# this is a single line comment.

Multi Line

'''
this is a multiline
comment.
'''

How to declare variables

a = 'some text'    # declare a string variable
b = 123            # an integer
c = True           # a boolean
d = "hello world"  # declare a string variable menggunakaan tanda ("") petik dua  
e = false          # a boolean
f = 3.5            # a float
g = 2+3j           # a complex
h = [1,2,3,4]      # a list
i = ("w","o","r","l","d") # a tuple

Conditional

def test_conditional():
    nama = "wenty"

    if(nama == "wenty"):
        print ("this is if")
    elif (nama == "tiara"):
        print ("this is elif")
    else:
        print("else")

test_conditional()
  • output :
    this is if

How to Looping

def test_looping():
    for x in range (0,5):
        print("nilai x:",x)

    i = 0
    while (i<5):
        print ("nilai i:",i)
        i += 1

test_looping()
  • output :
    nilai x: 0
    nilai x: 1
    nilai x: 2
    nilai x: 3
    nilai x: 4
    nilai i: 0
    nilai i: 1
    nilai i: 2
    nilai i: 3
    nilai i: 4

Function and Exception

def test_function_return_value(input_value):
    return input_value * 2

print(test_function_return_value(4))
print("-----------------")

def my_function(temp_string):
    print(temp_string + "<---")
    return temp_string + "done"

print(my_function("Test 1"))
print(my_function("Test 2"))
print(my_function("Test 3"))
print("-----------------")

def your_function(temp_string):
    try:
        result = temp_string + 5
    except TypeError:
        print(temp_string + "<---")
    finally:
        print("yeaaay")

your_function("test 1")
your_function("test 2")
  • output :
    8
    -----------------
    Test 1<---
    Test 1done
    Test 2<---
    Test 2done
    Test 3<---
    Test 3done
    -----------------
    test 1<---
    yeaaay
    test 2<---
    yeaaay

Namespace

global_var = 10
def test_namespace():
    private_var = 5
    global_var = 6

    print ("private_var :", private_var)
    print ("global_var :", global_var)

print("global_var:",global_var)
test_namespace()
  • output :
    global_var: 10
    private_var : 5
    global_var : 6

String Operator

def test_string_operator():
    full_string = "this is full string"
    temp_string = "temp string"
    upper_string = "THIS IS UPPER"
    lower_string = "this is lower"

    # Concat :
    print(full_string+temp_string)
    # Slicing :
    print(full_string[3:6])
    # Lower :
    print(upper_string.lower())
    # Upper :
    print(lower_string.upper())
    # Len :
    print(len(full_string))
    # Strip :
    print(full_string.strip("t"))
    # Replace :
    print(full_string.replace("full","name"))
    # Split
    print(full_string.split("is"))

test_string_operator()
  • output :
    this is full stringtemp string
    s i
    this is upper
    THIS IS LOWER
    19
    his is full string
    this is name string
    ['th', ' ', ' full string']