1 / 39

Python

Python. Aslı Ergün. Nasıl Çalıştırırım?. Komut satırından python yazın. Ve python ortamına geçin. Interpreter-Etkileşimli. % python >>> 3+3 6 Komut satırından komut calısıtırabilirsiniz. Ortamdan çıkmak için control-D veya exit() yazabilirsiniz. Python programı yazmak.

mahola
Download Presentation

Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Aslı Ergün

  2. Nasıl Çalıştırırım? • Komut satırından python yazın. Ve python ortamına geçin.

  3. Interpreter-Etkileşimli % python >>> 3+3 6 • Komut satırından komut calısıtırabilirsiniz. • Ortamdan çıkmak için control-D veya exit() yazabilirsiniz.

  4. Python programı yazmak • Programı aşağıdaki gibi derleriz: % python fact.py • Ama dosyayı once calısabilir hale getiririz: • Programımızın ilk satırına bunu yazarız: #!/usr/bin/python • Calısma modunu calıstırılabilir hale getiririz: % chmod a+x fact.py • Aşağıdaki gibi calıstırırız. % ./fact.py

  5. Hello Python • #!/usr/bin/python • print "Hello, Python!";

  6. Yorum Ve Açıklama • # işareti kullanarak acıklam ayapabilirsiniz: • # Program Adı: fact.py • # işaretini bazı komutları geçici olarak saklamak içinde kullanabilirsiniz. • #!/usr/bin/python • # Program Tanım • print "Hello, Python!"; # komut acıklama

  7. Değişkenler • Degisken adları bir sayı ile baslayamaz. • Degisken adları aritmetik isaretle baslayamaz • Degisken adları ya bir alfabe harfiyle ya da _ isaretiyle baslar. • Sorun yasamamak için değisken adlarında turkce karakter kullanılmaz • Program komut kelimeleri kullanılmaz.

  8. Print • Print (“bunları ekrana yazar”); • toplam=23; • Print(“toplam= “, toplam) • Print ile ekrana yazarken: • 1. Tek tırnak (‘ ‘) • 2. Çift tırnak (” ”) • 3. Üç tırnak (“”” “””) kullanabilirsiniz.

  9. Escape –Kaçış Karakterleri • print(“ortalama \n “)

  10. List - Diziler • a = ['sema', 'erol', ‘veli', ‘ayse’ ] • b= [40, 23, 100, 234] • b[2] = b[2] + 23 • b[0:2] = [1, 12] # aralıkta değiştirme • b[0:2] = [] #aralıkta silme • a[1:1] = [8, 77] #ekleme • a[:] = [] #dizi temizleme • len(a) #dizi boy

  11. İçiçe listeler • >>> q = [2, 3] • >>> p = [1, q, 4] • >>> p[1] • [2, 3] • >>> p[0]

  12. Karşılaştırmalar • == Eşittir • != Eşit Değil • < Kucuktur • > Buyuktur • <= Kucuk Eşit • >= Buyuk eşit.

  13. If • if <şartlar>: • komutlar • else: • komutlar

  14. must be a Boolean expression İf-else

  15. İf-elif-else • if <şartlar>: • komutlar • elif <şartlar>: • komutlar • else: • komutlar

  16. Mantık Baglaçları

  17. Operatörler

  18. Switch – Seçmeli Dizi Yapısı ile • def zero(): • print "Youtypedzero.\n" • def sqr(): • print "n is a perfectsquare\n" • def even(): • print "n is an evennumber\n" • def prime(): • print "n is a prime number\n“ • options = {0 : zero, •                 1 : sqr, •                 4 : sqr, •                 9 : sqr, •                 2 : even, •                 3 : prime, •                 5 : prime, •                 7 : prime, • } • secim =4 • Options[secim]

  19. Switch – Seçmeli Dizi Yapısı ile • def first_case(): • print "first" • def second_case(): • print "second" • def third_case(): • print "third" • mycase = { • 'first': first_case, #do not use () • 'second': second_case, #do not use () • 'third': third_case #do not use () • } • myfunc = mycase['first']

  20. Döngüler • For <değişken> in <aralık>: • for x in range(0, 3): • print "We're on time %d" % (x)

  21. For • for n in range(2, 10): • for x in range(2, n): • if n % x == 0: • print n, 'equals', x, '*', n/x • break • else: • # Asal Sayı bulur • print n, ‘asal sayıdır' ...

  22. Dizilerde For • for x in a[:]: • if x < 0: a.remove(x) • # Measure some strings: • words = ['cat', 'window', 'defenestrate'] • for w in words: • print w, len(w)

  23. While • # Fibonacci series: • # ilk 2 eleman toplamı 3. verir • a, b = 0, 1 • while b < 10: • print b • a, b = b, a+b

  24. while

  25. For-while karşılastırması

  26. a while True loop with a delayed exit loop’s termination condition causes an exit from the loop Dongu Kesme

  27. Atamalar

  28. Attırmalar

  29. Fonksiyonlar • def fib(n): • # write Fibonacci series up to n • """Print a Fibonacci series up to n.""" • a, b = 0, 1 • while a < n: • print a, • a, b = b, a+b • # Now call the function we just defined: • fib(2000)

  30. Fonksiyonlar Fact.py #! /usr/bin/python def fact(x): if x == 0: return 1 return x * fact(x - 1) print "\nN fact(N)" print "---------” for n in range(10): print n, fact(n)

  31. Lambda –Adsız Fonksiyon Kullanımı-1 • >>> def f (x): return x**2... >>> print f(8)64>>> >>> g = lambda x: x**2>>> >>> print g(8)64

  32. Lambda –Adsız Fonksiyon Kullanımı • result = { • 'a': lambda x: x * 5, • 'b': lambda x: x + 7, • 'c': lambda x: x - 2 • }[value](x)

  33. >>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]>>> >>> print filter(lambda x: x % 3 == 0, foo)[18, 9, 24, 12, 27]>>> >>> print map(lambda x: x * 2 + 10, foo)[14, 46, 28, 54, 44, 58, 26, 34, 64]>>> >>> print reduce(lambda x, y: x + y, foo)139>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]>>> >>> print filter(lambda x: x % 3 == 0, foo)[18, 9, 24, 12, 27]>>> >>> print map(lambda x: x * 2 + 10, foo)[14, 46, 28, 54, 44, 58, 26, 34, 64]>>> >>> print reduce(lambda x, y: x + y, foo)139

  34. Sınıflar • class Employee: • # Common base class for all employees empCount = 0 • def __init__(self, name, salary): • self.name = name • self.salary = salary • Employee.empCount += 1 • def displayCount(self): • print "Total Employee %d" % Employee.empCount • def displayEmployee(self): • print "Name : ", self.name, ", Salary: ", self.salary • emp1 = Employee("Zara", 2000) • emp2 = Employee("Manni", 5000)

More Related