Python: Difference between revisions

From Halfface
Jump to navigation Jump to search
 
(170 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? "))
  sys.argv  arguments are passed to the script in the variable sys.argv
=append=
#          is used to start a comment
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

Latest revision as of 09:18, 8 June 2023

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