Python: Difference between revisions

From Halfface
Jump to navigation Jump to search
 
(160 intermediate revisions by the same user not shown)
Line 1: Line 1:
=links=
=version=
  http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6
Show python version
  http://docs.python.org/tutorial
  python -V
Reference
=make an http request=
  http://docs.python.org/ref/ref.html
import requests
r =requests.get('https://halfface.se')
  print(r.text)
=dictionary=
  dictionary = {"a": 99, "hello": "world"}


=basics=
=import=
Import functions.
import $module
=try:=
Exception handling. Example will fail because x is unset.
try:
  print(x)
except:
  print("An exception occurred")
=sys.exit()=
exits and no stack traceback is printed.
=re=
Regular expression searching
ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', config_info)
=join elements in list for other output=
print(', '.join(configured_ip_addresses))
=for loop=
fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
=range=
for i in range(0,Loops,1):
    print("Value")


print "Hello, World!"
=input=
command called print followed by one argument,which is "Hello, World!". (referred to as a string of characters, or string) Command and its arguments are collectively referred to as a statement,
Read a value from prompt into variable number.
 
number=int(input("Please give me a number? "))
  print "2 + 2 is", 2 + 2
=append=
The first argument is the string "2 + 2 is" and the second argument is the mathematical expression 2 + 2, which is commonly referred to as an expression. each of the arguments separated by a comma
Append values to grades
 
  grades.append(int(input("Please give me the grade? ")))
sys.argv  arguments are passed to the script in the variable sys.argv
=format=
  #          is used to start a comment
Use variable i input question string.
=operations for numbers=
  sales = float(input("Please enter your sales from day {}".format(x)))
{| class="wikitable"
=round, number of decimals=
! Operation
print(round(AverageGrades),1)
! Symbol
=debug/trace=
! Example
python -m trace --trace script.py
|-
=virtualenvwrapper=
|Power (exponentiation)
enter
| <code>**</code>
  workon
| <code>5 ** 2 == 25</code>
exit
|-
  deactivate
|Multiplication
| <code>*</code>
|<code>2 * 3 == 6</code>
|-
|Division
| <code>/</code>
| <code>14 / 3 == 4</code>
|-
|Remainder (modulo)
| <code>%</code>
| <code>14 % 3 == 2</code>
|-
|Addition
| <code>+</code>
| <code>1 + 2 == 3</code>
|-
|Subtraction
| <code>-</code>
| <code>4 - 3 == 1</code>
|}
=userinput=
raw_input (returns a string)
user_reply = raw_input("Who goes there? ")
 
input (returns digits)
  number = input("Type in a number: ")
type(number) (tells what a variable is.)
  print "number is a", type(number)

Latest revision as of 10:51, 10 September 2024

version

Show python version

python -V

make an http request

import requests
r =requests.get('https://halfface.se')
print(r.text)

dictionary

dictionary = {"a": 99, "hello": "world"}

import

Import functions.

import $module

try:

Exception handling. Example will fail because x is unset.

try:
  print(x)
except:
  print("An exception occurred")

sys.exit()

exits and no stack traceback is printed.

re

Regular expression searching

ip = re.findall(r'[0-9]+(?:\.[0-9]+){3}', config_info)

join elements in list for other output

print(', '.join(configured_ip_addresses))

for loop

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

range

for i in range(0,Loops,1):
    print("Value")

input

Read a value from prompt into variable number.

number=int(input("Please give me a number? "))

append

Append values to grades

grades.append(int(input("Please give me the grade? ")))

format

Use variable i input question string.

sales = float(input("Please enter your sales from day {}".format(x)))

round, number of decimals

print(round(AverageGrades),1)

debug/trace

python -m trace --trace script.py

virtualenvwrapper

enter

workon

exit

deactivate