Python 3

Official Python Documentation

Python3 Tutorial

Python Concepts/Numbers

type

>>> type(6)
<class 'int'>

>>> type(6.4)
<class 'float'>
 
>>> type('6.4')
<class 'str'>

>>> type(b'6.4')
<class 'bytes'>

>>> type(['6.4'])
<class 'list'>

print

print
Formatted Output
PEP 378 — Format Specifier for Thousands Separator

### comma separated list of values
# sep=" " -> separated by blanks (default behaviour)
>>> print(q, p, p * q)
459 0.098 44.982

>>> print(q, p, p * q, sep=",")
459,0.098,44.982

>>> print(q, p, p * q, sep=" ! ")
459 ! 0.098 ! 44.982

# string concatenation
>>> print(str(q) + " " + str(p) + " " + str(p * q))
459 0.098 44.982

### The Old Way or the non-existing printf and sprintf
>>> print('Art: %5d, Price per Unit: %8.2f' % (453, 59.058))
Art:   453, Price per Unit:    59.06

>>> print("%#5x" % 12336)
0x3030

### The Pythonic Way: The string method "format"
>>> print('Art: {0:5d}, Price per Unit: {1:8.2f}'.format(453, 59.058))
Art:   453, Price per Unit:    59.06

>>> print('Art: {a:5d}, Price per Unit: {b:8.2f}'.format(a=453, b=59.058))
Art:   453, Price per Unit:    59.06
Art:   453, Price per Unit:    59.06

locale

Fastest way to thousands-commafy large numbers in Python/PyPy, 13 October 2012

Python locale error: unsupported locale setting
What is the correct way to set Python’s locale on Windows?
Language Strings
VS2017: setlocale
.NET Standard Numeric Format Strings

‘n’ Number. This is the same as ‘d’, except that it uses the current locale setting to insert the appropriate number separator characters.
# On Windows 10
>>> print(locale.getlocale())
('German_Switzerland', '1252')

>>> locale.setlocale(locale.LC_ALL, 'german-swiss')
'German_Switzerland.1252'

>>> x = 1234567890
>>> print("The value is {0:6,n}".format(x))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Cannot specify ',' with 'n'.

>>> print("The value is {0:6n}".format(x))
The value is 1234567890

Different Tutorials

An Introduction to Python Lists
Python – Command Line Arguments

IP-Address Validation

Regular Expressions in Python for dissecting IP

first, second, third, fourth = str(ipaddy).split('.')

How to validate IP address in Python?

>>> import ipaddress

>>> ipaddress.ip_address('127.0.0.1')
IPv4Address('127.0.0.1')

>>> ipaddress.ip_address('277.0.0.1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/ipaddress.py", line 54, in ip_address
    address)
ValueError: '277.0.0.1' does not appear to be an IPv4 or IPv6 address

>>> ipaddress.ip_address('foobar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/ipaddress.py", line 54, in ip_address
    address)
ValueError: 'foobar' does not appear to be an IPv4 or IPv6 address

21.28. ipaddress — IPv4/IPv6 manipulation library
`netaddr` is a Python library for representing and manipulating network addresses
netaddr 0.7.11 documentation
IP Address Tutorial (21.08.2009)
python 3: ipaddr/netaddr modules

String to Number

Convert strings to int or float in python 3?

try:
    integer = int(input('something: '))
    print('2 + {} = {}'.format(integer, integer + 2))
except ValueError as e:
    print("ooops - you didn't enter something I could make an int of...")

Leave a Reply

Your email address will not be published. Required fields are marked *