Python: Difference between revisions

From Halfface
Jump to navigation Jump to search
Line 30: Line 30:
   print(x)
   print(x)
=input=
=input=
Read a value from prompt
Read a value from prompt into variable number.
  number=int(input("Please give me a number? "))
  number=int(input("Please give me a number? "))
=append=
Append values to grades
grades.append(int(input("Please give me the grade? ")))

Revision as of 20:02, 24 August 2021

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)

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