Python: Basic Syntax

From OnnoWiki
Revision as of 08:59, 25 January 2021 by Onnowpurbo (talk | contribs) (Created page with "Bahasa Python memiliki banyak kesamaan dengan Perl, C, dan Java. Namun, ada beberapa perbedaan yang nyata dengan bahasa-bahasa tersebut. ==First Python Program== Mari kita...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Bahasa Python memiliki banyak kesamaan dengan Perl, C, dan Java. Namun, ada beberapa perbedaan yang nyata dengan bahasa-bahasa tersebut.


First Python Program

Mari kita jalankan program dalam berbagai mode pemrograman.

Interactive Mode Programming

Invoking the interpreter without passing a script file as a parameter brings up the following prompt −

$ python
Python 3.8.5 (default, Sep  4 2020, 07:30:14) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Type the following text at the Python prompt and press the Enter −

>>> print "Hello, Python!" If you are running new version of Python, then you would need to use print statement with parenthesis as in print ("Hello, Python!");. However in Python version 2.4.3, this produces the following result −

Hello, Python! Script Mode Programming Invoking the interpreter with a script parameter begins execution of the script and continues until the script is finished. When the script is finished, the interpreter is no longer active.

Let us write a simple Python program in a script. Python files have extension .py. Type the following source code in a test.py file −

Live Demo print "Hello, Python!" We assume that you have Python interpreter set in PATH variable. Now, try to run this program as follows −

$ python test.py This produces the following result −

Hello, Python! Let us try another way to execute a Python script. Here is the modified test.py file −

Live Demo

  1. !/usr/bin/python

print "Hello, Python!" We assume that you have Python interpreter available in /usr/bin directory. Now, try to run this program as follows −

$ chmod +x test.py # This is to make file executable $./test.py This produces the following result −

Hello, Python!