Monday, May 13, 2024

Python Beginner - Module 4 - Dave & Alison 01

Sekarang kita akan membahas Module 4 dari Alison Course dan Video ke 6 (6/24) dari Dave Gray. 


Image of Python for Beginners (credit to Google)

Functions
def myFunction():
    print('This is our function')

myFunction()
Run Python File:
This is our function

def betterPrint(string):
    for char in string:
        print(char)

betterPrint('hello')
Run Python File:
h
e
l
l
o

Which of these are the types of functions available in Python? Choose two.
- pre- defined function
- user defined function
-------------------------------------------

Variable Scope

Two types of Scope:
1) Global
- variable declared outside function

2) Local
variable declared inside function

x = 10
def function():
    global x
    x = 20

function()
print(x)
Run Python File:
20

True or False: Pre-defined scopes and user-defined scopes are the types of scopes available in Python.
- False
-----------------------------------------

Returning Data

Reverse
def reverse(string):
    print(string[::-1])

reverse('hello')
Run Python File
olleh


def reverse(string):
    return(string[::-1])

reverseString = reverse('hello')
print('data', reverseString)
Run Python File:
data olleh


Which of these refers to the factorial of 5? Choose three.
= 5 * 4!
= 5(4*3!)
= 5!
---------------------------------------------

Recursion

print(iterativeFactorial(3))
def iterativeFactorial(n):
    result = 1
    for i in range(n, 0, -1):
        result = result * i
    return result

print(iterativeFactorial(3))
Run Python File:
6 ...> 3*2*1 = 6

print(iterativeFactorial(5))
def iterativeFactorial(n):
    result = 1
    for i in range(n, 0, -1):
        result = result * i
    return result

print(iterativeFactorial(5))
Run Python File:
120 ...> 5*4*3*2*1 = 120


What do you call it when a function recalls itself several times for a purpose in Python programming? Drag the correct answer into the space provided.
- Recursion
--------------------------------------------


Tuples:
- immutable data

tuple ....> use ( )
tuple1 = (12, 13, 14, 15)
print(tuple1)
Run Python File:
(12, 13, 14, 15)

we can't change tuple assignment ...> immutable
tuple1 = (12, 13, 14, 15)
tuple1[1] = 16
Run Python File:
TypeError: 'tuple' object does not support item assignment


we can change tuple by list
tuple1 = (12,13,14,16)
list1 = list(tuple1)
list1[3] = 15 # Corrected index to 3 (since Python uses 0-based indexing)
tuple1 = tuple(list1)
print(tuple1)
Run Python File:
(12, 13, 14, 15)

Tuples in Python are denoted with what symbol? Choose one.
- answer: ( )
-------------------------------------

Dictionaries
We use "curl" bracket for dictionaries: { }
square backet for list: [ ]


employee = {
    'name' : 'John smith',
    'age' : 39,
    'salary' : '$10,000',
    'designation' : 'manager'
}

print(employee['name'], employee['salary'])
Run Python File:
John smith $10,000


for i in employee
employee = {
    'name' : 'John smith',
    'age' : 39,
    'salary' : '$10,000',
    'designation' : 'manager'
}

for i in employee:
    print(i)
Run Python File:
name
age
salary
designation


for key in employee
mployee = {
    'name' : 'John smith',
    'age' : 39,
    'salary' : '$10,000',
    'designation' : 'manager'
}

for key in employee:
    print(key + " : " + str(employee[key]))
Run Python File:
name : John smith
age : 39
salary : $10,000
designation : manager


We use "curl" bracket for dictionaries: { }
square backet for list: [ ]

The followings are the key points from this module:

The two types of Functions used in Python include: 
Predefined functions. 
User-defined functions. 

There are two types of scopes in Python and they are:
Global scope. 
Local scope. 

Recursion refers to when a function is called by itself over and over in order to achieve a result. 
Factorial, denoted by n!, is the product of all the numbers between 0 and that specific number. e.g. 4! = 4 * 3 * 2 * 1.
The factorial of 5, i.e 5!, can also be written as 5 * 4!.


Tuples are kind of lists that cannot be edited or amended but only read.
Tuples are denoted with ( ), instead of the [  ] as is the case in ordinary lists. 

Dictionaries are a list of key values which have specified value pair, where each key-value pairs are separated by a comma, and is denoted with the curly brackets ( {  } ).




=================================
===================================

Lesson from Dave Gray


Sekarang kita mempelajari lesson 6, yaitu List and Tuple.

users = ['Dave', 'John', 'Sara']

data = ['Dave', 42, True]

emptylist = []

print('Dave' in users)
print('Dave' in data)
print('Dave' in emptylist)
Run Python File:
True
True
False


users = ['Dave', 'John', 'Sara']

data = ['Dave', 42, True]

emptylist = []

print(users[0])
print(users[1])
print(users[-1])
print(users[-2])
Run Python File:
Dave
John...> 1
Sara
John...> -2


Users Index
users = ['Dave', 'John', 'Sara']

print(users.index('Dave'))
print(users.index('John'))
print(users.index('Sara'))
Run Python File:
0
1
2


Tambah (append) Elsa
users = ['Dave', 'John', 'Sara']

users.append('Elsa')
print(users)
Run Python File:
['Dave', 'John', 'Sara', 'Elsa']

Adding Jason, using +=
users = ['Dave', 'John', 'Sara']

users.append('Elsa')
print(users)

users += ['Jason']
print(users)
Run Python File:
['Dave', 'John', 'Sara', 'Elsa', 'Jason']

User extend
users = ['Dave', 'John', 'Sara']

users.append('Elsa')
print(users)

users += ['Jason']
print(users)

users.extend(['Robert', 'Jimmy'])
print(users)
Run Python File:
['Dave', 'John', 'Sara', 'Elsa', 'Jason']


Tuples ....> a kind of list that can't be changed or deleted
We use "curl" bracket for dictionaries: { }
square backet for list: [ ]
tuple: ( )

mytuple = tuple(('Dave', 42, True))

anothertuple = (1, 4, 2, 8)

print(mytuple)
print(type(mytuple))
print(anothertuple)
print(type(anothertuple))
Run Python File:
('Dave', 42, True)
<class 'tuple'>
(1, 4, 2, 8)
<class 'tuple'>




JJJJJJJJJJJJJJJJJJJJJJJ

JJJJJJJJJJJJJJJJJJJJJJJJJJJ

No comments:

Post a Comment