Actions

Difference between revisions of "Python for beginners"

From Algolit

(Literary Python for Beginners)
(LETTER * WORD * SENTENCE)
Line 5: Line 5:
 
* Introduction to the objects string & list with their different attributes
 
* Introduction to the objects string & list with their different attributes
 
* uses the shell or integrated development environment  
 
* uses the shell or integrated development environment  
 +
  
 
** run python 2.7 – installed by default on all platforms
 
** run python 2.7 – installed by default on all platforms

Revision as of 21:31, 14 November 2015

Literary Python for Beginners

LETTER * WORD * SENTENCE

  • Introduction to the objects string & list with their different attributes
  • uses the shell or integrated development environment


    • run python 2.7 – installed by default on all platforms

$ python

>>>

    • install python idle 2.7 – ways to interact with Python: shell + editor


# USING STRINGS

A string is a chain of characters / text and can contain any type of characters

A string is defined by " "

* write letter using STRING

>>> "a"

* write several letters

>>> "a, b, c, d"

* write combinations

>>> "a" + "b"

>>> 3

>>> "a" + "and" + "b"

>>> "a" + " and " + "b"

>>> "a" + "+ " + 3 + " is a3" ***

>>> 3*3

>>> "a" + "+ " + "3" + " is a3"

>>> 3 * "algolit" + " in wtc25"


    • Exercise: Write 'I write the alphabet 3 times.'

Note: there are always different possible solutions


* write string as variable

    • Avoids having to retype your string each time you use it
    • You can change values at any time of the writing process

>>> letter = "a"

>>> print letter

>>> word = "algolit"

>>> print word

>>> sentence = "I learn to read and write again at wtc25"

>>> print sentence, letter

    • Exercise: print your letter, word, sentence


* add punctuation

>>> print letter + " " + word + " " + sentence + "."

>>> print letter + "! " + word + "? " + sentence + "."

>>> letter = "i"

>>> print letter + "! " + word + "? " + sentence + "."

    • Exercise: change content of one of variables, over and over, see how result changes


* calculate!

    • the length of the string

>>> len(letter) >>> len(word) >>> len(sentence)


    • we can also consider the letter/word/sentence as fields or grids, in which each letter occupies a specific position

Note: computer starts to count from 0

>>> word[0] >>> word[3]

    • Exercise: what is the middle letter of your sentence?


* Slicing and concatenating strings in order to select letters & compose new words

    • find last letter

>>> zin[-1]

    • find last but one letter

>>> word[-2]

    • find first two letters

>>> word[0:2]

or

>>> word[:2]

    • find 3 letters in the middle

>>> word[2:5]

    • find from 3rd letter till end

>>> word[2:]

    • Exercise: If the word is "solidarity" is, what do you read here?

word[:5] + word[3:]

    • Exercise: rewrite the word as 'liquidity', using slicing


* write with capitals

>>> print letter.lower()

>>> print sentence.upper()


* write first word of sentence with capital letter

>>> print sentence.capitalize()


* note the difference

>>> sentence.title()

>>> word.title()


Some_Python_Vocabulary

Loops_and_conditions

Anthology