Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Pertemuan 1 - Introduction to Python

Python is an interpreted programming language created by Guido van Rossum in 1991, designed for rapid development and system integration. It supports various applications, including scripting and automation, and features a simple syntax that allows for easy variable definition and data type management. Key concepts include lists, tuples, dictionaries, and input/output handling, making Python versatile for tasks ranging from web development to machine learning.

Uploaded by

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

Pertemuan 1 - Introduction to Python

Python is an interpreted programming language created by Guido van Rossum in 1991, designed for rapid development and system integration. It supports various applications, including scripting and automation, and features a simple syntax that allows for easy variable definition and data type management. Key concepts include lists, tuples, dictionaries, and input/output handling, making Python versatile for tasks ranging from web development to machine learning.

Uploaded by

10aisahpayapo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

What is 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

Applications of Scripting Languages :


● To automate certain tasks in a program
● Extracting information from a data set
● Less code intensive as compared to traditional programming languages

● Applications of Programming Languages :


● They typically run inside a parent program like scripts
● More compatible while integrating code with mathematical models
● Languages like JAVA can be compiled and then used on any platform
Why we learn to Python?

1. Easy to develop
2. Can create to develop
many products (Website,
Desktop, and Machine
Learning Model)
How to create Python Script

• We can write in editor


like notepad, and etc to
save python script.
• To create python script,
you can save using
notepad as
script_name.py (all
extensions) or run a
command python
script_name.py.
5 Rule to Write Python

• How to Write Statement


• String
• Naming Convention
• Programming Block of
Statement
• Comment
Write a statement

• 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

• Python is case sensitive


means you need to comply
on capital or lower case
(e.g: judul, Judul)
• If you defined a variable
name as judul, and you call
your variable as Judul, it
will show up an error
(variable is not found)
• Python is using snake case
to define variable means
every words must split by
underscore ( _ ) (e.g:
angka_pertama, sisa_bagi)
Naming

• Python use CamelCase


to define class
• To define constant
variable (variable that
will be used in one file)
Python use ALL CAPS
and snake_case
combined (e.g: PI)
Block statement

• 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

• Comment is one or many lines


that will be not execute to give
any knowledge about what is
python script file using for to
another programmer.
• Comment also can be used for
skip any instructions in Python.
• There are many ways to write a
comment in python:
1. Use number sign/hash symbol
(#).
2. Use triple of number sign/hash
symbol (###).
3. Use double quote or single quote
(“ “) atau (‘ ‘).
4. Use triple of double quote (“””
“””).
Variable and Data Type in Python

• 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

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ] Output:


['abcd', 786, 2.23, 'john', 70.2]
tinylist = [123, 'john'] abcd
[786, 2.23]
[2.23, 'john', 70.2]
print list # Prints complete list [123, 'john', 123, 'john']
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']
print list[0] # Prints first element of the list
print list[1:3] # Prints elements starting from 2nd till 3rd
print list[2:] # Prints elements starting from 3rd element
print tinylist * 2 # Prints list two times
print list + tinylist # Prints concatenated lists
Tuple

• 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

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 ) OUTPUT:


('abcd', 786, 2.23, 'john', 70.2)
tinytuple = (123, 'john') abcd
(786, 2.23)
(2.23, 'john', 70.2)
print tuple # Prints complete list (123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.2, 123, 'john')
print tuple[0] # Prints first element of the list
print tuple[1:3] # Prints elements starting from 2nd till 3rd
print tuple[2:] # Prints elements starting from 3rd element
print tinytuple * 2 # Prints list two times
print tuple + tinytuple # Prints concatenated lists
Dictionary

• 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

• Variable can be defined


with format
(nama_variabel =
<nilai>).
• We can see what is
inside its variable with
using command print.
How to convert data type

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.

float(x) Converts x to a floating-point number.


complex(real [,imag]) Creates a complex number.

str(x) Converts object x to a string representation.


repr(x) Converts object x to an expression string.
eval(str) Evaluates a string and returns an object.
tuple(s) Converts s to a tuple.
list(s) Converts s to a list.
set(s) Converts s to a set.
dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s) Converts s to a frozen set.
chr(x) Converts an integer to a character.
unichr(x) Converts an integer to a Unicode character.
ord(x) Converts a single character to its integer value.
hex(x) Converts an integer to a hexadecimal string.
oct(x) Converts an integer to an octal string.
Input - Output

• This is how we are


processing tasks
• Input is prerequisites of
doing task
• Process is how to do
task
• Output is result of task
Input – Output (Example)

• 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

• To show teks in our


console use print()
command.
• print() command must
insert with string. To
combine between string
and variable, python
must use (+).
Use string format()

• format() is use to concat


between variable and
text menggabungkan isi
variabel dengan teks.
• {} on sentence will be
replaced by value of
variable nama.

You might also like