User Tools

Site Tools

tools:python

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tools:python [2024/06/03 05:54] – [If] Humphrey Boa-Garttools:python [2025/10/12 10:56] (current) Humphrey Boa-Gart
Line 1: Line 1:
-{{wst>iw-cleanup}} +#redirect tools:languages:python
- +
-====== Python ====== +
- +
-Below applies to Python 2.6 or lower, Python 3 is a good bit different. +
- +
-Python is an object oriented, interpreted language. It is unique in that it requires a user to indent in place of using curly brackets ({..}). +
- +
-Note: that most *nix distributions come with Python by default. +
- +
-===== Basics ===== +
- +
-//Basic// python syntax +
- +
-==== Hello World! ==== +
- +
-  >>> print "HELLO WORLD!" +
-  HELLO WORLD! +
- +
-==== Variables ==== +
- +
-Variable names DO NOT need to begin with a special character, unlike PHP. Variable names CAN, however, be any combination, as long as the word isn't reserved (Ex: and, or, print), of letters and numbers. +
- +
-  >>> #Numbers +
-  >>> a = 1 #Declare A as 1 +
-  >>> b = a+5 #b becomes 6, while a remains 1 +
-  >>> a += 5 #a is overwritten, and becomes 6 +
-  >>> c = b = a #c and b are overwritten and become 6 +
-  >>> #Strings +
-  >>> a = "Hello " +
-  >>> b = "There" +
-  >>> c = a+b #JOIN STRINGS +
-  >>> a = "Blue Yellow Green Red".split(" ") #Split the string at every " " +
-  >>> a #Output 'a' to the screen, same as print or print() +
-  ['Blue', 'Yellow', 'Green', 'Red'] #List object, another type of variable +
-  >>> .join(a) #Join all the list objects as one +
-  'BlueYellowGreenRed' +
-  >>> a = 'Blue Green Yellow Red'[0:4] #This returns the sub-string 'Blue' +
-  >>> a = 'Blue Green Yellow Red'[5:] #This returns the sub-string 'Green Yellow Red' +
-  >>> a = 'a = 'Blue Green Yellow Red'[::2] #This returns the sub-string Bu re elwRd' +
-  >>> #Float  +
-  >>> a = 88.2 +
-  >>> b = 88 +
-  >>> print a/25 +
-  3.528 +
-  >>> print b/25 +
-  3 +
-  >>> #Other types +
-  >>> a = (" ", 355, 256.7, ["Hello", "World"]) +
-  >>> type(a) +
-  <type 'tuple'> +
-  >>> a = {"Hello":"World", "Key":"Value"+
-  >>> type(a) +
-  <type 'dict'> +
-  >>> a = "Anhero" +
-  >>> del a #Its a good idea, if you're making a 'hidden' script, to delete variables after you use them... +
-   +
-===== Statements ===== +
- +
-==== Comparison ==== +
- +
-  <       Less than +
-  >       Greater than +
-  ==      Equal to +
-  !=      Not equal +
-  is      Identical +
-  is not  Not identical +
-  <=      Less than or equal to +
-  >=      Greater than or equal to +
- +
-=== If === +
- +
-  >>> a = 1 +
-  >>> b = 2 +
-  >>> if a != b: +
-  print "No!" +
-   +
-   +
-  No! +
-  >>> a = 1 +
-  >>> b = 1 +
-  >>> if a == b: +
-  print "Yes!" +
-    +
-    +
-  Yes! +
-  >>> a = 1 +
-  >>> b = 5 +
-  >>> if (a == b-len("....")/1) and "a" == "a" or "b" == "b": +
-  print "Yes!" +
-   +
-   +
-  Yes! +
- +
-==== Loops ==== +
- +
-=== While === +
- +
-  >>> a = 1 +
-  >>> while a < 5: #Note, replacing '<' with '<=' allows it to reach 5, instead of stopping at 4 +
- print a +
- a += 1; +
-   +
-  1 +
-  2 +
-  3 +
-  4 +
-  >>> a = False; +
-  >>> while a == False: +
- print "False" +
- a = True; +
-    +
-   +
-  False +
- +
-=== For === +
- +
-  >>> for x in range(0, 10): +
-  print x +
-   +
-   +
-  0 +
-  1 +
-  2 +
-  3 +
-  4 +
-  5 +
-  6 +
-  7 +
-  8 +
-  9 +
- +
-//consider using xrange when dealing with larger lists, as it creates an iterator, rather than constructing the full list in memory// +
- +
-  >>> for x in ['Hello', 'world', ',this', 'is', 'a', 'list']: +
- print x +
-   +
-     +
-  Hello +
-  world +
-  ,this +
-  is +
-  a +
-  list +
- +
-{{tag>Tools Programming}}+
tools/python.1717394087.txt.gz · Last modified: (external edit)

Find this page online at: https://bestpoint.institute/tools/python