hello friends! new(ish)!

Python: Difference between revisions

From InstallGentoo Wiki v2
Jump to navigation Jump to search
>Oz
No edit summary
>Philharmonic
No edit summary
 
(14 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Python is a wicked cool [sic] interpreted scripting language that a lot of people start their programming journeys with. It has an easy to read, dynamically typed syntax that appeals to beginners and people who suffer from great sloth.
[[File:Python.png|thumb|Python logo.]]
'''Python''' is a wicked cool [sic] interpreted scripting [[Programming languages |language]] that a lot of people start their programming journeys with. It has an easy to read, dynamically typed syntax that appeals to beginners and people who suffer from great sloth.
 
==Origin==
Python was first an idea in the 1980s, with its main author being [[Wikipedia:Guido_van_Rossum |Guido van Rossum]]. Until 2018 he was the benevolent dictator for life (BDFL) because of his pivotal role in deciding the direction of Python. It was originally designed as a teaching language, and ease of learning still guides much of it's development.  


==Basic Examples==
==Basic Examples==
The basics of Python are to start a file with the path of the interpreter. If you have written code in BASH, Perl or Ruby you will recognize the opening line.
Python code is often compared to pseudo-code, because of it's simple and intuitive syntax. The following program will print numbers from 1 to 10:
 
    for i in range(1, 11):
        print(i)
 
There are several ways to run it. If you have idle installed, you can press F5 to run a script. If you are working at a command line, you might type:


     #!/usr/bin/env python
     $ python3 my_file.py


To run your python application, you have two methods. If you are using a UNIX based operating system you can use the `chmod` command to make the file executable, or pass it as an argument to the python interpreter.
To be able to run it like you would a bash script or executable, make the following line the first line of the file:


     $ chmod +x my_file.py && ./my_file.py
     #!/usr/bin/env python3


Alternatively:
Then use the `chmod` command to make the file executable.


     $ python my_file.py
     $ chmod +x my_file.py && ./my_file.py


===Printing===
===Printing===
Printing to the screen is always the most basic introduction to a programming language.
Printing to the screen is always the most basic introduction to a programming language.


     #!/usr/bin/python
     #!/usr/bin/env python
      
      
     print("Hello World!")
     print("Hello World!")
Line 24: Line 33:
Variables are a way of representing data with human-readable names.
Variables are a way of representing data with human-readable names.


     #!/usr/bin/python
     #!/usr/bin/env python
      
      
     my_variable = "Hello "
     my_variable = "Hello "
     print(my_variable + "World!")
     print(my_variable + "World!")


As you can see, we used the variable in the print() function and ''concatinated'' it with the rest of the string "Wolrd!".
As you can see, we used the variable in the print() function and ''concatenated'' it with the rest of the string "World!".


===Arithmetic===
===Arithmetic===
Python, like all languages, can do math as well.
Python, like all languages, can do math as well.


     #!/usr/bin/python
     #!/usr/bin/env python
      
      
     num = 5
     num = 5
Line 44: Line 53:
* / (division)
* / (division)
* * (multiplication)
* * (multiplication)
* % (modulus)
* ** (exponentiation)
In addition, Python supports augmented assignment:
    num = num + 5
    num += 5 # equal to the above statement
    num = num / 5
    num /= 5 # equal to the above statement
Augmented assignment operators are available for each standard mathematical operator:
* += (addition)
* -= (subtraction)
* /= (division)
* *= (multiplication)
...and so on. As a rule of thumb, an augmented assignment operator can be constructed by taking its equivalent mathematical operator and appending an equals sign: + becomes +=, - becomes -=, etc.
== Python 2 and 3 ==
The two currently used versions of Python are Python 2.x and Python 3.x. While Python 3 was released more recently (in late 2008), many libraries, frameworks, and other dependencies use the older version. As a result, many programmers have not made the switch and dependencies continue being written for Python 2.x. While it is recommended that programmers use Python 3.x now, use of Python 2.x is still acceptable due to third-party support.


[[Category:Programming]]
[[Category:Programming languages]]
[[Category:HowTo]]
[[Category:Tutorials]]

Latest revision as of 06:00, 3 April 2019

Python logo.

Python is a wicked cool [sic] interpreted scripting language that a lot of people start their programming journeys with. It has an easy to read, dynamically typed syntax that appeals to beginners and people who suffer from great sloth.

Origin

Python was first an idea in the 1980s, with its main author being Guido van Rossum. Until 2018 he was the benevolent dictator for life (BDFL) because of his pivotal role in deciding the direction of Python. It was originally designed as a teaching language, and ease of learning still guides much of it's development.

Basic Examples

Python code is often compared to pseudo-code, because of it's simple and intuitive syntax. The following program will print numbers from 1 to 10:

   for i in range(1, 11):
       print(i)

There are several ways to run it. If you have idle installed, you can press F5 to run a script. If you are working at a command line, you might type:

   $ python3 my_file.py

To be able to run it like you would a bash script or executable, make the following line the first line of the file:

   #!/usr/bin/env python3

Then use the `chmod` command to make the file executable.

   $ chmod +x my_file.py && ./my_file.py

Printing

Printing to the screen is always the most basic introduction to a programming language.

   #!/usr/bin/env python
   
   print("Hello World!")

Variables

Variables are a way of representing data with human-readable names.

   #!/usr/bin/env python
   
   my_variable = "Hello "
   print(my_variable + "World!")

As you can see, we used the variable in the print() function and concatenated it with the rest of the string "World!".

Arithmetic

Python, like all languages, can do math as well.

   #!/usr/bin/env python
   
   num = 5
   print(num + 6)

The above program will display the sum of 5 and 6 (11, you pleb). You can use any of the following operators:

  • + (addition)
  • - (subtraction)
  • / (division)
  • * (multiplication)
  • % (modulus)
  • ** (exponentiation)

In addition, Python supports augmented assignment:

   num = num + 5
   num += 5 # equal to the above statement
   num = num / 5
   num /= 5 # equal to the above statement

Augmented assignment operators are available for each standard mathematical operator:

  • += (addition)
  • -= (subtraction)
  • /= (division)
  • *= (multiplication)

...and so on. As a rule of thumb, an augmented assignment operator can be constructed by taking its equivalent mathematical operator and appending an equals sign: + becomes +=, - becomes -=, etc.

Python 2 and 3

The two currently used versions of Python are Python 2.x and Python 3.x. While Python 3 was released more recently (in late 2008), many libraries, frameworks, and other dependencies use the older version. As a result, many programmers have not made the switch and dependencies continue being written for Python 2.x. While it is recommended that programmers use Python 3.x now, use of Python 2.x is still acceptable due to third-party support.