Python/Basic Code

From r00tedvw.com wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
;variables
+
=variables=
 
:variables can be integers, floats, or boolean.  Integers are whole numbers, floats are numbers with remainders, boolean is either True of False.
 
:variables can be integers, floats, or boolean.  Integers are whole numbers, floats are numbers with remainders, boolean is either True of False.
 
  variable=integer
 
  variable=integer
:convert int to string
+
==float vars==
 +
If you want the fraction included in the var, you need to define it as a float, otherwise python will automatically round the result to a whole number.
 +
<nowiki> floating_var = float(7)/2</nowiki>
 +
This will give you the fraction from dividing 7/2.
 +
==convert int to string==
 
  <nowiki>age = 13
 
  <nowiki>age = 13
 
print str(age)</nowiki>
 
print str(age)</nowiki>
:convert string to int
+
==convert string to int==
 
  <nowiki>n1 = "100"
 
  <nowiki>n1 = "100"
 
n2 = "10"
 
n2 = "10"
 
int_addition = int(n1) + int(n2)</nowiki>
 
int_addition = int(n1) + int(n2)</nowiki>
:convert to float
+
==convert to float==
 
  <nowiki>n1 = "7.5"
 
  <nowiki>n1 = "7.5"
 
print float(n1)</nowiki>
 
print float(n1)</nowiki>
;arithmetic operators
+
=arithmetic operators=
:add/subtract/multiply/divide/exponentiation/modulo
+
==add/subtract/multiply/divide/exponentiation/modulo==
:exponentiation (exponential, similar to square roots)
+
===exponentiation (exponential, similar to square roots)===
 
  2 ** 3=8  (2*2*2)
 
  2 ** 3=8  (2*2*2)
:modulo (remainder)
+
===modulo (remainder)===
 
  5 % 2=1  (5/2 = 2 with a remainder of 1)
 
  5 % 2=1  (5/2 = 2 with a remainder of 1)
:shorthand
+
===shorthand===
 
  <nowiki>+= add vars and update first var
 
  <nowiki>+= add vars and update first var
 
ie. sandwich_price += sales_tax
 
ie. sandwich_price += sales_tax
Line 24: Line 28:
 
-= subtract vars and update first var
 
-= subtract vars and update first var
 
ie. money_in_wallet -= sandwich_price</nowiki>
 
ie. money_in_wallet -= sandwich_price</nowiki>
:float vars
+
 
If you want the fraction included in the var, you need to define it as a float, otherwise python will automatically round the result to a whole number.
+
=comments=
<nowiki> floating_var = float(7)/2</nowiki>
+
This will give you the fraction from dividing 7/2.
+
;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
 
  #quote
 
  #quote
 
  """quote
 
  """quote
 
  quote"""
 
  quote"""
;print
+
=print=
:display output
+
==display output==
 
  print "Hello World"
 
  print "Hello World"
:display specific letter, left to right, 0 forward
+
==display specific letter, left to right, 0 forward==
 
  print "TEST"[0] = "T"
 
  print "TEST"[0] = "T"
;multi-line string
+
=strings=
 +
==multi-line string==
 
:for strings that span multiple lines, use triple quotes
 
:for strings that span multiple lines, use triple quotes
 
  <nowiki>address_string = """136 Whowho Rd
 
  <nowiki>address_string = """136 Whowho Rd
 
Apt 7
 
Apt 7
 
Whosville, WZ 44494"""</nowiki>
 
Whosville, WZ 44494"""</nowiki>
;string continuation
+
==string continuation==
 
:it appears you can continue a string by using <code>\</code> though i've only seen this behavior using string concatenation
 
:it appears you can continue a string by using <code>\</code> though i've only seen this behavior using string concatenation
 
  <nowiki>ie.  
 
  <nowiki>ie.  
 
print "Ah, so your name is %s, your quest is %s, " \
 
print "Ah, so your name is %s, your quest is %s, " \
 
"and your favorite color is %s." % (name, quest, color)</nowiki>
 
"and your favorite color is %s." % (name, quest, color)</nowiki>
;remove character value
+
==string methods==
: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() ; lower() ; upper() ; str()
:len() = get length of string  -  applies to anything within the parentheses
+
===len() = get length of string  -  applies to anything within the parentheses===
 
  len(length)  =  6
 
  len(length)  =  6
:lower() = convert to lowercase  -  applies to only the value after the "." .  Dot notation only works on strings
+
===lower() = convert to lowercase  -  applies to only the value after the "." .  Dot notation only works on strings===
 
  LOWERCASE.lower()  =  lowercase
 
  LOWERCASE.lower()  =  lowercase
:upper() = convert to uppercase  -  applies to only the value after the "." . Dot notation only works on strings
+
===upper() = convert to uppercase  -  applies to only the value after the "." . Dot notation only works on strings===
 
  uppercase.upper()  =  UPPERCASE
 
  uppercase.upper()  =  UPPERCASE
:str() = convert to string  -  applies to anything within the parentheses
+
===str() = convert to string  -  applies to anything within the parentheses===
 
  str(3.14)  =  "3.14"
 
  str(3.14)  =  "3.14"
;String Formatting
+
==String Formatting==
 
:concatenating strings through short hand
 
:concatenating strings through short hand
 
  <nowiki>string_1 = "Camelot"
 
  <nowiki>string_1 = "Camelot"
 
string_2 = "place"
 
string_2 = "place"
 
 
print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)</nowiki>
 
print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)</nowiki>
;user input
+
==user input==
 
:variable = raw_input("display message")
 
:variable = raw_input("display message")
 
  name=raw_input("display message")
 
  name=raw_input("display message")
;Comparators
+
==slice==
 +
:parse certain characters from a variable
 +
<nowiki>x=variable
 +
print x[1:3]
 +
ari
 +
 +
x=variable
 +
print [3:len(x)]
 +
iable</nowiki>
 +
 
 +
=remove character special 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!'
 +
=Comparators=
 
:<nowiki>==, !=, <, <=, >, or >=</nowiki>
 
:<nowiki>==, !=, <, <=, >, or >=</nowiki>
;Boolean Operators
+
==Boolean Operators==
 
:and, or, or not
 
:and, or, or not
:true, false
+
===true, false===
 
  True and True is True
 
  True and True is True
 
  True and False is False  
 
  True and False is False  
Line 87: Line 98:
 
  Not True is False
 
  Not True is False
 
  Not False is True
 
  Not False is True
;Conditional Statements
+
==Conditional Statements==
 
:if, else, and elif.
 
:if, else, and elif.
 
  def clinic():
 
  def clinic():
Line 102: Line 113:
 
   
 
   
 
  clinic()
 
  clinic()
 
;slice
 
:parse certain characters from a variable
 
x=variable
 
print x[1:3]
 
ari
 
 
x=variable
 
print [3:len(x)]
 
iable
 

Revision as of 16:55, 30 October 2019

Contents

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

float vars

If you want the fraction included in the var, you need to define it as a float, otherwise python will automatically round the result to a whole number.

 floating_var = float(7)/2

This will give you the fraction from dividing 7/2.

convert int to string

age = 13
print str(age)

convert string to int

n1 = "100"
n2 = "10"
int_addition = int(n1) + int(n2)

convert to float

n1 = "7.5"
print float(n1)

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"""

print

display output

print "Hello World"

display specific letter, left to right, 0 forward

print "TEST"[0] = "T"

strings

multi-line string

for strings that span multiple lines, use triple quotes
address_string = """136 Whowho Rd
Apt 7
Whosville, WZ 44494"""

string continuation

it appears you can continue a string by using \ though i've only seen this behavior using string concatenation
ie. 
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)

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 "." . Dot notation only works on strings

LOWERCASE.lower()   =   lowercase

upper() = convert to uppercase - applies to only the value after the "." . Dot notation only works on strings

uppercase.upper()   =   UPPERCASE

str() = convert to string - applies to anything within the parentheses

str(3.14)   =   "3.14"

String Formatting

concatenating strings through short hand
string_1 = "Camelot"
string_2 = "place"
print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

user input

variable = raw_input("display message")
name=raw_input("display message")

slice

parse certain characters from a variable
x=variable
 print x[1:3]
 ari
 
 x=variable
 print [3:len(x)]
 iable

remove character special 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!'

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()
Personal tools
Namespaces

Variants
Actions
Navigation
Mediawiki
Confluence
DevOps Tools
Open Source Products
Ubuntu
Ubuntu 22
Mac OSX
Oracle Linux
AWS
Windows
OpenVPN
Grafana
InfluxDB2
TrueNas
OwnCloud
Pivotal
osTicket
OTRS
phpBB
WordPress
VmWare ESXI 5.1
Crypto currencies
HTML
CSS
Python
Java Script
PHP
Raspberry Pi
Canvas LMS
Kaltura Media Server
Plex Media Server
MetaSploit
Zoneminder
ShinobiCE
Photoshop CS2
Fortinet
Uploaded
Certifications
General Info
Games
Meal Plans
NC Statutes
2020 Election
Volkswagen
Covid
NCDMV
Toolbox