Posts

Showing posts with the label programming

CHAPTER 5. CONDITIONALS, LOOPS, AND SOME OTHER STATEMENTS

print  python 2 -statement, python 3 -function Arguments of print do not form Tuples >>> 1,2,3 (1, 2, 3) >>> print 1,2,3 1 2 3 >>> print (1,2,3) (1, 2, 3) ________________________________ >>> name = 'Gumby' >>> salutation = 'Mr.' >>> greeting = 'Hello,' >>> print greeting, salutation, name Hello, Mr. Gumby ________________________________ Issues about comma and space before >>> name = 'Gumby' >>> name = 'Gumby' >>> salutation = 'Mr.' >>> greeting = 'Hello' >>> print greeting,',',salutation,name Hello , Mr. Gumby >>> print greeting+',',salutation,name Hello, Mr. Gumby ------------------------------------------ print 'Hello,', print 'world!' print out Hello, world! ________________________________ >>> import math as foobar >>> foo...

Chapter 4.Dictionaries. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

DICTIONARIES telephone numbers (and other numbers that may contain leading zeros) should be represented as strings of digits—not integers. There are keys and values,closed in a curly braces/ >>> phonebook = {'Alice':'1111','Beth':'2222','Cecil':'3333'} >>> phonebook['Cecil'] '3333' ______________________________________________ dict function >>> items = [('GG','FF'),('11',22)] >>> items = dict(items) >>> items {'GG': 'FF', '11': 22} It can be used with keyword arguments >>> d = dict (name = "Gumby", age  = 42) >>> d {'age': 42, 'name': 'Gumby'} _________________________________ BASIC DICTIONARY OPERATIONS >>> d = dict(apple = 'gavno',android = 'fuflo') >>> d {'android': 'fuflo', 'apple': ...

Chapter 3.Working With Strings. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

String formatting operator >>> print format % value Hello, Pit, How do you do? >>> format = 'Hello, %s. %s do you do?' >>> values = ('Pit','How') >>> print format % values Hello, Pit. How do you do? ____________________________________ formatting real numbers (floats) >>> format = 'Pi with four decimals: %.4f' >>> from math import pi >>> print format % pi Pi with four decimals: 3.1416 _______________________________________ Template strings/Template method SUBSTITUTE >>> from string import Template >>> s = Template('$x, glorious $x') >>> s.substitute(x = 'slurm') 'slurm, glorious slurm' ___________________________ If the replacement field is part of a word, x should be enclosed in braces >>> from string import Template >>> s = Template("It's ${x}tastic") >>> s.substitute(x=...

Chapter 2. LISTS & TUPLES. 'Beginning Python From Novice to Professional' by Magnus Lie Hetland

FUNCTIONS of LISTS >>> list ('Hello') ['H', 'e', 'l', 'l', 'o']' __________________ Item assignments >>> x = [1,1,1] >>> x[1]=2 >>> x [1, 2, 1] _________________________ Deleting elements >>> names = ['debora','Alice','Trinity','Sofia'] >>> del names[2] >>> names ['debora', 'Alice', 'Sofia'] _______________________________________ Slicing >>> name = list('Perl') >>> name ['P', 'e', 'r', 'l'] >>> name[2:]=list('ar') >>> name ['P', 'e', 'a', 'r'] ________________________________________ Different length of initial and final list after slicing >>> name[1:]=list('ython') >>> name ['P', 'y', 't', 'h', 'o', 'n...