Posts

Showing posts from August, 2014

Python's readability

Image

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...

Chapter 2. LISTS & TUPLES.SEQUENCES 'Beginning Python From Novice to Professional'

>>> ed = ['Edward Snowden',42] >>> steve = ['Steven Jobs',55] >>> gven = ['Gven Stephany',35] >>> barak = ['Barak Obama',56] >>> database = [ed,steve,gven,barak] >>> database [['Edward Snowden', 42], ['Steven Jobs', 55], ['Gven Stephany', 35], ['Barak Obama', 56]] >>> _________________________________________________ Common Sequence Operations These operations include indexing, slicing, adding, multiplying, and checking for membership . INDEXING >>>print 'losos'[4] s >>> 'losos'[-1] 's' >>> third = raw_input('Who? ')[3] Who? Kozel >>> third 'e' ____________________________________________ >>> endings = ['st', 'nd', 'rd'] + 17 * ['th'] \ + ['st', 'nd', 'rd'] + 7 * ['th'] \ + ['st...

Chapter 1 Numbers & Expressions, 'Beginning Python From Novice to Professional' / Цифры и выражения

Addition, subtraction, сложение, вычитание >>> 2+2 4 >>>3-2 1 (!) Division, деление >>>1/2 0 >>>1.0/2.0 0,5 >>>1/2. 0.5 _______________________________________________________________________________ From _future_import division >>>1/2 0.5 >>>1//2 0 _______________________________________________________________________________ Reminder operator / оператор остатка >>>1%2 1 >>> 10%3 1 >>> 10%4 2 >>> 10%5 0 _______________________________________________________________________________ Multiplication / умножение >>> 3*333 999 _______________________________________________________________________________ Exponentiation / Возведение в степень >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9 _______________________________________ Variables Assignment x = 3 Expression x*2 ________________________________...

'HELLO WORLD' or starting with 'Beginning Python From Novice to Professional', Second Edition, Magnus Lie Hetland

I've started to read an incredible manual on Python by Magnus Lie Hetland. My very intelligent friend 've adviced and he is right - very nice book. Some of practical issues I will post sometimes to remind myself becouse it's hard to keep in mind some elements of new  language ))) So... >>>print 'Hello world'