Actions

Python for beginners

From Algolit

Revision as of 14:21, 21 November 2020 by An (talk | contribs)

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