Pertemuan 1 - Introduction to Python
Pertemuan 1 - Introduction to Python
• Python is interpreter
language Guido van
Rossum, released in
1991.
• Python is a
programming language
that lets you work more
quickly and integrate
your systems more
effectively. (python.org)
What is Python?
Python is interpreted
language, what is interpreter
language?
Scripting vs Compiling Language
1. Easy to develop
2. Can create to develop
many products (Website,
Desktop, and Machine
Learning Model)
How to create Python Script
• Statement is instruction
or command that will be
executed by computer.
• Python use semicolon
(;) to divide two
statements in one line.
String
• String is compound of
character.
• String is written by
using double quote (“ ”)
or single quote (‘ ’).
• You can enter new line
on string by using triple
of double quote (“””
“””)
Naming Convention
• Block is list of
instructions that will be
run in one place.
• Block is written by using
tab or four times of
blank space (to make a
different of code).
Comment
• Variable is a place to
store a data, meanwhile
data type is content
that we store.
• Variable is mutable
means its value can be
changed.
Data Type in Python
List
• Lists are the most versatile of Python's compound data types. A list contains items
separated by commas and enclosed within square brackets ([]).
• To some extent, lists are similar to arrays in C. One difference between them is
that all the items belonging to a list can be of different data type.
• The values stored in a list can be accessed using the slice operator ( [ ] and [ : ] )
with indexes starting at 0 in the beginning of the list and working their way to end-
1.
• The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the
repetition operator.
List
• A tuple is another sequence data type that is similar to the list. A tuple consists of
a number of values separated by commas. Unlike lists, however, tuples are
enclosed within parentheses.
• The main differences between lists and tuples are: Lists are enclosed in brackets ( [
] ), and their elements and size can be changed, while tuples are enclosed in
parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only
lists.
Tuple
• Python 's dictionaries are hash table type. They work like associative arrays or
hashes found in Perl and consist of key-value pairs.
• Keys can be almost any Python type, but are usually numbers or strings. Values, on
the other hand, can be any arbitrary Python object.
• Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and
accessed using square braces ( [] ).
Dictionary
dict = {} OUTPUT:
This is one
dict['one'] = "This is one" This is two
dict[2] = "This is two“ {'dept': 'sales', 'code': 6734, 'name': 'john'}
['dept', 'code', 'name']
tinydict = {'name': 'john','code':6734, 'dept': 'sales'} ['sales', 6734, 'john']
print dict['one'] # Prints value for 'one' key
print dict[2] # Prints value for 2 key
print tinydict # Prints complete dictionary
print tinydict.keys() # Prints all the keys
print tinydict.values() # Prints all the values
Define a variable
Function Description
int(x [,base]) Converts x to an integer. base specifies the base if x is a string.
long(x [,base] ) Converts x to a long integer. base specifies the base if x is a string.
• Eating:
○ Input: Food
○ Process: Human Digestion
System
○ Output: Energy
Input – Output (Example)
• Calculator:
○ Input: Numbers
○ Process: Math Operations
(Addition, Substraction,
Multiply, Divison)
○ Output: Number
How to get input from keyboard
• Python
use input() and raw_inp
ut() to take inputs from
keyboard.
• Python3 not using
raw_input() anymore
meanwhile are input will
be treated as string.
• If we define variable to
store input means its
variable will store
anything that we type
on keyboard.
Output