Python (programming language)

Python is an open-source programming language. It was made as a language that is both easy to work on and understand.[30] It was made by a Dutch programmer named Guido van Rossum in 1991, who named it after the television program Monty Python's Flying Circus.

Python (programming language)
Python-logo-notext.svg
Paradigm(s)Multi-paradigm: object-oriented,[1] procedural (imperative), functional, structured, reflective
Appeared in20 February 1991; 35 years ago (1991-02-20)[2]
Designed byGuido van Rossum
DeveloperPython Software Foundation
Typing disciplineDuck, dynamic, strong typing;[3] gradual (since 3.5, but ignored in CPython)[4]
Major implementationsCPython, PyPy, Stackless Python, MicroPython, CircuitPython, IronPython, Jython
DialectsCython, RPython, Starlark[5]
Influenced byABC,[6] Ada,[7] ALGOL 68,[8] APL,[9] C,[10] C++,[11] CLU,[12] Dylan,[13] Haskell,[14] Icon,[15] Java,[16] Lisp,[17] Modula-3,[11] Perl, Standard ML[9]
InfluencedApache Groovy, Boo, Cobra, CoffeeScript,[18] D, F#, Genie,[19] Go, JavaScript,[20][21] Julia,[22] Nim, Ring,[23] Ruby,[24] Swift,[25] V (Vlang)[26]
OSWindows, Linux/UNIX, macOS and more[27]
LicensePython Software Foundation License
Usual filename extensions.py, .pyi, .pyc, .pyd, .pyo (prior to 3.5),[28] .pyw, .pyz (since 3.5)[29]
Websitewww.python.org
Wikibooks logo Python Programming at Wikibooks

Python is an interpreted language. This means it does not need to be compiled before running. A program called an interpreter runs Python code on almost any computer. So, a programmer can change the code and quickly see what happens. But this also makes Python slower than compiled languages like C, because it is not changed into machine code before running. Instead, this happens while the program is running.

Python use

Python is usually used for making websites, automating common tasks, and making charts and graphs. Since it's simple to learn, people who are not computer experts, like bookkeepers and researchers, often learn Python.

Its standard library is made up of many functions that come with Python when it is installed.[31] On the Internet, there are many other libraries, which have been examined to provide wonderful ends in varied areas like Machine Learning (ML), Deep Learning, etc.[32] These libraries make it a powerful language; it can do many different things.

Python's developers try to avoid changing the language to make it better until they have a lot of things to change. Also, they try not to make small repairs, called patches, to unimportant parts of CPython, the main version of Python, even if the patches would make it faster. When speed is important, a Python programmer can write some of the program in a different language, like C, or use PyPy, a different kind of Python that uses a just-in-time compiler.

Keeping Python fun to use is an important goal of Python’s developers. It reflects in the language's name, a tribute to the British comedy group Monty Python. Tutorials often take a playful approach, such as referring to spam and eggs instead of the standard foo and bar.

Syntax

Some of Python's syntax comes from C, because that is the language that Python was written in. But Python uses whitespace to delimit code: spaces or tabs are used to organize code into groups. This is different from C. In C, there is a semicolon at the end of each line and curly braces ({}) are used to group code. Using whitespace to delimit code makes Python a very easy-to-read, but hard to manage language.[33]

Statements and control flow

In Python, the assignment statement <syntaxhighlight lang="python" inline>myVariable = 42</syntaxhighlight> means that the variable myVariable is set to the integer 42. The type of myVariable is not needed: Python dynamically adapts its type to the given value. For example, replace 42 with the string 'spam'. The statement <syntaxhighlight lang="python" inline>myVariable = 'spam'</syntaxhighlight> would work, but it wouldn't in another language like C or C++.

Python's statements include the following keywords:

  • The if statement runs its first block of code if its condition is met. The optional elif (a contraction of else if) statement runs its block of code if the previous conditions are not met, but its condition is met. The else statement runs the last block of code if none of the previous conditions are met.
  • The while statement is a loop. As long as its condition is met, it runs its block of code.
  • The for statement is a loop with an index. It iterates over a list and binds each element to the index to use in that block.
  • The def statement defines a function or method.
  • The pass statement does nothing.
  • The class statement allows the users to create their own type of objects like what integers and strings are.
  • The import statement imports Python files for use in the user's code.
  • The print statement outputs a message to the console.

Expressions

Python's expressions include some operators that are similar to other programming languages and others that are not:

  • Addition +, subtraction -, multiplication *, and division /.
  • Exponents, represented by **.
  • To compare two values, the condition uses ==.
  • Python uses the words and, or, and not for its boolean expressions.
  • To check if a value is in a list, Python uses in.

Example

The following Python program shows "Hello World!" on the screen:

<syntaxhighlight lang="python3" line> print("Hello World!") </syntaxhighlight>

A comment starts with # until the end of the line: <syntaxhighlight lang="python3" line> print("Hello World!") # displays: Hello World! </syntaxhighlight>

When a variable is modified by a number or a word, the programmer does not have to say what is the type of the variable. The assignment (modification) of the variable is dynamic. This makes it easier to reuse the variable, making fast changes simpler. An example of this is shown below. This code will display the number 42 then the string "Hello World!", using the same variable.

<syntaxhighlight lang="python3" line highlight="4" copy> myVariable = 42 print(myVariable) # displays: 42

myVariable = "Hello World!" print(myVariable) # displays: Hello World! </syntaxhighlight>

In another language like C, a programmer would have to declare whether myVariable is a number or a string before modify it. An error will occur if one changes a number to a string.

Python has easy-to-use functions. The C language requires the type of the function. In Python, the function does not need a type after :. <syntaxhighlight lang="python3" line highlight="1" copy> def greeting(name):

   return "Hello, " + name

</syntaxhighlight> When called with the parameter name as "World!", this function returns the string "Hello, World!".

<syntaxhighlight lang="python3" line start="3" copy> print(greeting("World!")) # displays: Hello, World! </syntaxhighlight>

Versions

Python (programming Language) Media

References

  1. General Python FAQ — Python 3.9.2 documentation. docs.python.org. Retrieved 2021-03-28.
  2. Python 0.9.1 part 01/21alt.sources archives. Retrieved 2021-08-11.
  3. Why is Python a dynamic language and also a strongly typed language - Python Wiki. wiki.python.org. Retrieved 2021-01-27.
  4. PEP 483 -- The Theory of Type Hints. Python.org. Retrieved 14 June 2018.
  5. Starlark Language. Retrieved 25 May 2019.
  6. Why was Python created in the first place?. General Python FAQPython Software Foundation. Retrieved 22 March 2007.
  7. Ada 83 Reference Manual (raise statement). Retrieved 7 January 2020.
  8. Kuchling, Andrew M.. Interview with Guido van Rossum (July 1998). amk.ca (22 December 2006). Retrieved 12 March 2012.
  9. 9.0 9.1 itertools — Functions creating iterators for efficient looping — Python 3.7.1 documentation. docs.python.org. Retrieved 22 November 2016.
  10. van Rossum, Guido. An Introduction to Python for UNIX/C Programmers. Proceedings of the NLUUG Najaarsconferentie (Dutch UNIX Users Group) (1993).
  11. 11.0 11.1 Classes. The Python TutorialPython Software Foundation. Retrieved 20 February 2012.
  12. Lundh, Fredrik. Call By Object. effbot.org. Retrieved 21 November 2017.
  13. Simionato, Michele. The Python 2.3 Method Resolution OrderPython Software Foundation. Retrieved 29 July 2014.
  14. Kuchling, A. M.. Functional Programming HOWTO. Python v2.7.2 documentationPython Software Foundation. Retrieved 9 February 2012.
  15. Schemenauer, Neil. PEP 255 – Simple Generators. Python Enhancement Proposals (18 May 2001)Python Software Foundation. Retrieved 9 February 2012.
  16. Smith, Kevin D.. PEP 318 – Decorators for Functions and Methods. Python Enhancement Proposals (2 September 2004)Python Software Foundation. Retrieved 24 February 2012.
  17. More Control Flow Tools. Python 3 documentationPython Software Foundation. Retrieved 24 July 2015.
  18. CoffeeScript. coffeescript.org. Retrieved 3 July 2018.
  19. The Genie Programming Language Tutorial. Retrieved 28 February 2020.
  20. Perl and Python influences in JavaScript. www.2ality.com (24 February 2013). Retrieved 15 May 2015.
  21. Rauschmayer, Axel. Chapter 3: The Nature of JavaScript; Influences. O'Reilly, Speaking JavaScript. Retrieved 15 May 2015.
  22. Why We Created Julia. Julia website (February 2012). Retrieved 5 June 2014.
  23. Ring Team. Ring and other languages. ring-lang.net (4 December 2017)ring-lang. Retrieved 4 December 2017.
  24. Bini, Ola. Practical JRuby on Rails Web 2.0 Projects: bringing Ruby on Rails to the Java platform (2007). Berkeley: APress. p. 3. ISBN 978-1-59059-881-8.
  25. Lattner, Chris. Chris Lattner's Homepage (3 June 2014)Chris Lattner. Retrieved 3 June 2014.
  26. V Documentation. Retrieved 13 January 2024.
  27. Download Python (in en). Python.org. Retrieved 2021-05-24.
  28. File extension .pyo was removed in Python 3.5. See PEP 0488 Archived 1 June 2020 at the Wayback Machine
  29. Holth, Moore. PEP 0441 -- Improving Python ZIP Application Support (30 March 2014). Retrieved 12 November 2015.
  30. Simpliv. Learn Python Programming Fundamentals: A Beginner's Guide (2020).
  31. Python Coding | Data Basecamp (in en-US) (2021-11-27). Retrieved 2022-08-29.
  32. Yadav, Pramod. Top 25 Python Libraries For Data Science Projects – My Programming School. My Programming School (2020-10-09). Retrieved 2022-12-22.
  33. Sah, Rocky. How Python parses white space. Codelivly - A Way For Coders (2022).

Other websites