Difference between revisions of "Python"

From neil.tappsville.com
Jump to navigationJump to search
(Created page with " ==== Python version of a perl hash ==== <pre> import collections ; def tree() : return collections.defaultdict(tree) hash = tree() hash'outer_key''inner key' = "v...")
 
m
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
==== Data Types ====
 +
<pre>
 +
int 1234
 +
>>> type(1234)
 +
<class 'int'>
 +
float 55.50
 +
>>> type(55.50)
 +
<class 'float'>
 +
complex
 +
>>> type(6+4j)
 +
<class 'complex'>
 +
string hellow
 +
>>> type("hello")
 +
<class 'str'>
 +
Square list [1,2,3,4]
 +
>>> type([1,2,3,4])
 +
<class 'list'>
 +
Round tuple (1,2,3,4)
 +
>>> type((1,2,3,4))
 +
<class 'tuple'>
 +
Curly dict {1:"one", 2:"two", 3:"three"}
 +
>>> type({1:"one", 2:"two", 3:"three"})
 +
<class 'dict'>
 +
</pre>
 +
  
 
==== Python version of a perl hash ====
 
==== Python version of a perl hash ====
Line 15: Line 40:
 
; else : print "Hash has some data"
 
; else : print "Hash has some data"
 
</pre>
 
</pre>
 +
 +
=== Strings ===
 +
 +
* Use <code>"'{0}' is longer than '{1}'".format(name1, name2)</code>
 +
 +
* The new-style {} formatting uses {} codes and the .format method 
 +
* <code>'It will cost ${0} dollars.'.format(95)</code>
 +
* The old-style % formatting uses % codes for formatting: 
 +
* <code>'It will cost $%d dollars.' % 95</code>
 +
* Note that with old-style formatting, you have to specify multiple arguments using a tuple: 
 +
*  <code>'%d days and %d nights' % (40, 40)</code>
 +
 +
The 'new'  / safe way to concatenate strings in a print statement
 +
print("%s is %d years old." % (name, age))
 +
 +
===XML===
 +
 +
https://www.datacamp.com/community/tutorials/python-xml-elementtree
 +
 +
 +
== Update Modules==
 +
 +
sudo python3 -m pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 sudo python3 -m pip install -U

Latest revision as of 06:14, 1 June 2020

Data Types

 
int 1234
>>> type(1234)
<class 'int'>
float 55.50
>>> type(55.50)
<class 'float'>
complex
>>> type(6+4j)
<class 'complex'>
string hellow
>>> type("hello")
<class 'str'>
Square list [1,2,3,4]
>>> type([1,2,3,4])
<class 'list'>
Round tuple (1,2,3,4)
>>> type((1,2,3,4))
<class 'tuple'>
Curly dict {1:"one", 2:"two", 3:"three"}
>>> type({1:"one", 2:"two", 3:"three"})
<class 'dict'>


Python version of a perl hash

import collections

; def tree() : return collections.defaultdict(tree)

hash = tree()

hash[['outer_key']][['inner key']] = "value"


; if not bool(hash) : print "Hash is empty"
; else : print "Hash has some data"

Strings

  • Use "'{0}' is longer than '{1}'".format(name1, name2)
  • The new-style {} formatting uses {} codes and the .format method
  • 'It will cost ${0} dollars.'.format(95)
  • The old-style % formatting uses % codes for formatting:
  • 'It will cost $%d dollars.' % 95
  • Note that with old-style formatting, you have to specify multiple arguments using a tuple:
  • '%d days and %d nights' % (40, 40)

The 'new' / safe way to concatenate strings in a print statement

print("%s is %d years old." % (name, age))

XML

https://www.datacamp.com/community/tutorials/python-xml-elementtree


Update Modules

sudo python3 -m pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 sudo python3 -m pip install -U