hello friends! new(ish)!
Python: Difference between revisions
>Tibs m (logo) |
>Arch-angel |
||
Line 46: | Line 46: | ||
* * (multiplication) | * * (multiplication) | ||
* % (modulus) | * % (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. | |||
[[Category:Programming languages]] | [[Category:Programming languages]] |
Revision as of 05:40, 5 March 2014
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.
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.
#!/usr/bin/env python
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.
$ chmod +x my_file.py && ./my_file.py
Alternatively:
$ python 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.