Actions

Difference between revisions of "Python for beginners"

From Algolit

Line 1: Line 1:
 
== Literary Python for Beginners ==
 
== Literary Python for Beginners ==
  
** run python 2.7 – installed by default on all platforms (Ubuntu, Mac, Windows)
+
Next pages: [[Python_for_beginners/loops_and_conditions|Loops and Conditions]] // [[Python_for_beginners/anthology|Create anthology]]
  
$ python
+
=== VARIABLES ===
 
 
>>>
 
 
 
** install python idle 2.7 – ways to interact with Python: shell + editor
 
 
 
** you can also use independent editors, such as Bluefish editor or Sublime text (not FLOSS! Lime is being developed). Note: commenting code in BLuefish: Ctrl-Alt-C
 
 
 
 
 
Next pages: [[Python for beginners/some vocabulary|Some vocabulary]] // [[Python_for_beginners/loops_and_conditions|Loops and Conditions]] // [[Python_for_beginners/anthology|Create anthology]]
 
 
 
=== LETTER * WORD * SENTENCE ===
 
  
 
* 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 editor
  
  
Line 26: Line 15:
 
A string is defined by " "
 
A string is defined by " "
  
'''* write letter using STRING'''
+
'''* Write text using STRING'''
 +
 
 +
>>> print("La Cambre")
  
>>> "a"
+
** Exercise: Write your name'''
  
'''* write several letters'''
+
>>> ...
  
>>> "a, b, c, d"
+
'''* Adding text'''
  
'''* write combinations'''
+
>>> print("Brussels"+"Paris")
  
>>> "a" + "b"  
+
>>> print("Brussels "+"Paris")
  
>>> 3
 
  
>>> "a" + "and" + "b"
+
** Exercise: Write your address'''
  
>>> "a" + " and " + "b"
+
'''* Composing a sentence'''
  
>>> "a" + "+ " + 3 + " is a3" ***
+
>>> print("Paris", "to", "London", "via", "Brussels")
  
>>> 3*3
+
>>> print("Paris to London via Brussels")
  
>>> "a" + "+ " + "3" + " is a3"
+
** Exercise: Write your favourite expression'''
  
>>> 3 * "algolit" + " in wtc25"
+
'''* Multiply'''
  
 +
>>> print(3*3)
  
** Exercise: Write 'I write the alphabet 3 times.'
+
>>> 3 * "algolit" + " in Brussels"
 +
 
 +
** Exercise: Write 'I write the alphabet' 3 times.
  
 
Note: there are always different possible solutions  
 
Note: there are always different possible solutions  
Line 65: Line 58:
 
>>> letter = "a"  
 
>>> letter = "a"  
  
>>> print letter  
+
>>> print(letter)
  
 
>>> word = "algolit"  
 
>>> word = "algolit"  
  
>>> print word  
+
>>> print(word)
  
>>> sentence = "I learn to read and write again at wtc25"  
+
>>> sentence = "I learn to read and write again in Python."  
  
>>> print sentence, letter
+
>>> print(sentence, letter)
  
 
** Exercise: print your letter, word, sentence
 
** Exercise: print your letter, word, sentence
Line 80: Line 73:
 
'''* add punctuation'''
 
'''* add punctuation'''
  
>>> print letter + " " + word + " " + sentence + "."  
+
>>> print(letter + " " + word + " " + sentence + ".")
  
>>> print letter + "! " + word + "? " + sentence + "."  
+
>>> print(letter + "! " + word + "? " + sentence + ".")
  
 
>>> letter = "i"  
 
>>> letter = "i"  
Line 91: Line 84:
  
  
'''* calculate!'''
+
'''What you've learned'''
 
+
 
** the length of the string
+
* variable
 
+
* value
>>> len(letter)
+
* assignment operator (=)
>>> len(word)
+
* difference between variables and values
>>> len(sentence)
+
* integers
 
+
* print()
 
 
** 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()
 
 
 
 
 
 
 
'''From STRINGS to LISTS'''
 
'''
 
* LISTS are MUTABLE vs immutable STRINGS'''
 
 
 
That is the reason you want to change your string into a list:
 
 
 
>>> sentence.split()
 
 
 
and save it as a new variable:
 
 
 
>>> list_words = sentence.split()  
 
 
 
'''* some of the attributes of string can be used for lists'''
 
** Exercise: which previously mentioned attributes function for lists?
 
 
 
 
 
'''* replace a word in a list'''
 
 
 
>>> list_words[3] = "wtc25"
 
 
 
>>> print list_words
 
 
 
 
 
'''* remove words'''
 
 
 
>>> list_words[2:3] = []
 
 
 
Note: list_words[2] = [] → this removes your 3rd element and replaces it by an empty sublist ('nested list')
 
 
 
if you know the word, you can also use list_words.remove(word)
 
 
 
'''* put back words'''
 
 
 
>>> list_words[2:3] = ["so", "exciting"]
 
 
 
 
 
'''* shorten a list'''
 
 
 
>>> list_words[-3:]
 
 
 
* and save the shortened list into a new variable – the old continues to exist
 
 
 
>>> yezzzz = list_words[-3:]
 
 
 
 
 
'''* combine lists, group two in one'''
 
 
 
>>> text = [list_words, yezzzz]
 
 
 
 
 
'''* call an element of a sublist'''
 
 
 
>>> word = text[1][2]
 
 
 
 
 
'''* concatenate lists, merge two into one'''
 
 
 
>>> print(list_words + yezzzz)
 
 
 
>>> poem = list_words + yezzzz
 
 
 
 
 
'''*add words to your list'''
 
 
 
>>> poem.append("is")
 
>>> print(poem)
 
 
 
 
 
'''* sort your list alphabetically'''
 
 
 
>>>  poem.sort()
 
 
 
 
 
'''* transform your list into a string again'''
 
 
 
>>> " ".join(poem)
 
 
 
NOTE: the whitespace / try without
 
 
 
>>> " ".join(poem) + "?"
 
 
 
More information on list operators: https://docs.python.org/2/tutorial/datastructures.html
 
 
 
* empty your list // you have a brand new sheet of paper :-)
 
 
 
>>> poem[:] = []
 

Revision as of 14:21, 21 November 2020

Literary Python for Beginners

Next pages: Loops and Conditions // Create anthology

VARIABLES

  • Introduction to the objects string & list with their different attributes
  • uses the shell or editor


# USING STRINGS

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

A string is defined by " "

* Write text using STRING

>>> print("La Cambre")

    • Exercise: Write your name

>>> ...

* Adding text

>>> print("Brussels"+"Paris")

>>> print("Brussels "+"Paris")


    • Exercise: Write your address

* Composing a sentence

>>> print("Paris", "to", "London", "via", "Brussels")

>>> print("Paris to London via Brussels")

    • Exercise: Write your favourite expression

* Multiply

>>> print(3*3)

>>> 3 * "algolit" + " in Brussels"

    • 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 in Python."

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


What you've learned

  • variable
  • value
  • assignment operator (=)
  • difference between variables and values
  • integers
  • print()