beberapa materi tentang python

4
PROKOM – 10 April 2015 >>> type (17) <type 'int'> contoh: >>> int (18.8700) 17 >>> float (17) 17.0 contoh: >>> sqrt (2) Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> sqrt (2) NameError: name 'sqrt' is not defined >>> import math >>> sqrt (2) Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> sqrt (2) NameError: name 'sqrt' is not defined >>> math.sqrt (9) 3.0 Membuat Fungsi Contoh: >>> def tulis_lirik ( ) : ... print "bintang kecil" ... print "di langit yang tinggi" ... >>> tulis_lirik ( ) bintang kecil di langit yang tinggi >>> def tulis_lirik2 ( ) : ... tulis_lirik ( ) ... tulis_lirik ( ) ... >>> tulis_lirik2 ( ) bintang kecil

Upload: johan-faiz

Post on 20-Dec-2015

5 views

Category:

Documents


0 download

DESCRIPTION

materi didapatkan saat kelas program komputer

TRANSCRIPT

Page 1: Beberapa materi tentang python

PROKOM – 10 April 2015

>>> type (17)<type 'int'>

contoh:>>>int (18.8700)17>>> float (17)17.0

contoh:>>> sqrt (2)Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> sqrt (2)NameError: name 'sqrt' is not defined>>> import math>>> sqrt (2)Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> sqrt (2)NameError: name 'sqrt' is not defined>>> math.sqrt (9)3.0

Membuat Fungsi

Contoh:>>> def tulis_lirik ( ) :... print "bintang kecil"... print "di langit yang tinggi"...>>> tulis_lirik ( )bintang kecildi langit yang tinggi

>>> def tulis_lirik2 ( ) :... tulis_lirik ( )... tulis_lirik ( )...>>> tulis_lirik2 ( )bintang kecildi langit yang tinggibintang kecildi langit yang tinggi

Page 2: Beberapa materi tentang python

Import dengan From

Contoh :>>> piTraceback (most recent call last): File "<pyshell#0>", line 1, in <module> piNameError: name 'pi' is not defined

>>> math.piTraceback (most recent call last): File "<pyshell#1>", line 1, in <module> math.piNameError: name 'math' is not defined

>>> from math import pi>>> print pi3.14159265359>>> 2*pi6.283185307179586

Parameters

Contoh :>>> def print_twice(bruce) :... print bruce... print bruce...>>> print_twice("bintang")bintangbintang

contoh :>>> bintang="a little star, not only at all">>> print_twice("bintang")bintangbintang

>>> print_twice(bintang)a little star, not only at alla little star, not only at all

Page 3: Beberapa materi tentang python

Problem : Conversion TableSuppose we want to make a table conversion from celcius to fahrenheit

Solution #1>>> C = -20>>> F = 9.0/5*C + 32>>> print C,F-20 -4.0

>>> C=-20; F=9.0/5*C+32; print C,F-20 -4.0

Solution #2

While Loops

print '_____' # table headingC = -20 # start value for CdC = 5 # increment of C in loopwhile C <= 40 : # loop heading with condition F = (9.0/5)*C + 32 # 1st statement inside loop C = C + dC # 2nd statement inside loop print C, F # 3rd statement inside loopprint '_____' # end

Solution #3

For Loops

Cdegrees = [-20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40]print 'C F'for C in Cdegrees : F = (9.0/5)*C + 32 print C, F

Boolean ExpressionAn expression with true or false value is called a Boolean Expression

Branching-conditionals

Contoh :#conditional ifno=float (input('masukkan angka ?"))if no>0 : print ("angka positif itu bro")print ("ini ujungnya program")

Page 4: Beberapa materi tentang python

#conditional if elseno=float (input (“masukkan angka ?"))if no>=0 : print ("angka positif atau nol itu bro")else : print ("negatif")