Informatika2-2014/Gyakorlat01

A MathWikiből
(Változatok közti eltérés)
78. sor: 78. sor:
 
</python>
 
</python>
 
=== Feladatok ===
 
=== Feladatok ===
 
+
==== Python interpreter ====
 +
* Nyisd meg a Python interpretert parancssorban
 +
* Számold ki az alábbi összefüggések értékét
 +
** 12 + 96
 +
** (x^2 + 4x + 4), ha x = 0, x = 1, x = 2 ,x = 10
 +
** sqrt(6), sqrt(9)
 +
==== 1. Python program ====
 +
* Írdd meg első Python programodat az elméleti bevezetőben leírtak alapján. A program írjon
 
=== Házi Feladat ===
 
=== Házi Feladat ===
 
[[Informatika2-2014|Hazi01]]
 
[[Informatika2-2014|Hazi01]]

A lap 2014. február 10., 14:23-kori változata

Tartalomjegyzék

Python

Elmélet

  • Altalános körben használható
  • Magas szintű programozási nyelv
  • Egyik alap elve az olvasható kód írása tiszta szintaxis használatával
  • Dinamikus interpreter nyelv
  • Sokszor script nyelvként használjuk
  • "Pythonban minden objektum"
  • PEP8
ez_egy_hosszu_fuggvenynev(ez_egy_hosszu_valtozo)
OsztalyNev
  • Python interpreter futtatása:
$ python        ## Run the Python interpreter
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 6       ## set a variable in this interpreter session    
>>> a           ## entering an expression prints its value
6
>>> a + 2
8
>>> a = 'hi'    ## a can hold a string just as well
>>> a     
'hi'
>>> len(a)      ## call the len() function on a string
2
>>> foo(a)      ## try something that doesn't work
Traceback (most recent call last):
  File "", line 1, in ?
NameError: name 'foo' is not defined
>>> ^D          ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
  • Python program:
#!/usr/bin/python
 
# import modules used here -- sys is a very standard one
import sys
 
# Gather our code in a main() function
def main():
    print 'Hello there', sys.argv[1]
    # Command line args are in sys.argv[1], sys.argv[2] ...
    # sys.argv[0] is the script name itself and can be ignored
 
# Standard boilerplate to call the main() function to begin
# the program.
if __name__ == '__main__':
    main()
$ python hello.py Guido
Hello there Guido
$ ./hello.py Alice    # without needing 'python' first (Unix)
Hello there Alice
  • Függvények:
# Defines a "repeat" function that takes 2 arguments.
def repeat(s, exclaim):
    """Returns the string s repeated 3 times.
    If exclaim is true, add exclamation marks.
    """
 
    result = s + s + s # can also use "s * 3" which is faster (Why?)
    if exclaim:
        result = result + '!!!'
    return result

Feladatok

Python interpreter

  • Nyisd meg a Python interpretert parancssorban
  • Számold ki az alábbi összefüggések értékét
    • 12 + 96
    • (x^2 + 4x + 4), ha x = 0, x = 1, x = 2 ,x = 10
    • sqrt(6), sqrt(9)

1. Python program

  • Írdd meg első Python programodat az elméleti bevezetőben leírtak alapján. A program írjon

Házi Feladat

Hazi01

Személyes eszközök