Python/Basic Code
From r00tedvw.com wiki
(Difference between revisions)
Line 11: | Line 11: | ||
<nowiki>+= add vars and update first var | <nowiki>+= add vars and update first var | ||
ie. sandwich_price += sales_tax | ie. sandwich_price += sales_tax | ||
+ | |||
-= subtract vars and update first var | -= subtract vars and update first var | ||
− | ie. money_in_wallet -= sandwich_price | + | ie. money_in_wallet -= sandwich_price</nowiki> |
;comments | ;comments | ||
:<nowiki>#</nowiki> for single like quote or """ quote """ for multi line quote | :<nowiki>#</nowiki> for single like quote or """ quote """ for multi line quote |
Revision as of 13:45, 30 October 2019
- variables
- variables can be integers, floats, or boolean. Integers are whole numbers, floats are numbers with remainders, boolean is either True of False.
variable=integer
- arithmetic operators
- add/subtract/multiply/divide/exponentiation/modulo
- exponentiation (exponential, similar to square roots)
2 ** 3=8 (2*2*2)
- modulo (remainder)
5 % 2=1 (5/2 = 2 with a remainder of 1)
- shorthand
+= add vars and update first var ie. sandwich_price += sales_tax -= subtract vars and update first var ie. money_in_wallet -= sandwich_price
- comments
- # for single like quote or """ quote """ for multi line quote
#quote """quote quote"""
- display output
print "Hello World"
- display specific letter, left to right, 0 forward
print "TEST"[0] = "T"
- remove character value
- like with regular expression, you can remove a characters normal value by adding \ in front, however this does not work with all characters in python
'Help! Help! I\'m being repressed!'
- string methods
- len() ; lower() ; upper() ; str()
- len() = get length of string - applies to anything within the parentheses
len(length) = 6
- lower() = convert to lowercase - applies to only the value after the .
LOWERCASE.lower() = lowercase
- upper() = convert to uppercase - applies to only the value after the .
uppercase.upper() = UPPERCASE
- str() = convert to string - applies to anything within the parentheses
str(3.14) = "3.14"
- user input
- variable = raw_input("display message")
name=raw_input("display message")
- Comparators
- ==, !=, <, <=, >, or >=
- Boolean Operators
- and, or, or not
- true, false
True and True is True True and False is False False and True is False False and False is False True or True is True True or False is True False or True is True False or False is False Not True is False Not False is True
- Conditional Statements
- if, else, and elif.
def clinic(): print "You've just entered the clinic!" print "Do you take the door on the left or the right?" answer = raw_input("Type left or right and hit 'Enter'.").lower() if answer == "left" or answer == "l": print "This is the Verbal Abuse Room, you heap of parrot droppings!" elif answer == "right" or answer == "r": print "Of course this is the Argument Room, I've told you that already!" else: print "You didn't pick left or right! Try again." clinic() clinic()
- slice
- parse certain characters from a variable
x=variable print x[1:3] ari x=variable print [3:len(x)] iable