Python for beginners: Difference between revisions
From Algolit
(→**VARIABLES**) |
|||
Line 2: | Line 2: | ||
− | === | + | === VARIABLES === |
* Introduction to the objects string & list with their different attributes | * Introduction to the objects string & list with their different attributes | ||
Line 15: | Line 15: | ||
− | + | ==== Write text using STRING ==== | |
>>> print("La Cambre") | >>> print("La Cambre") | ||
Line 24: | Line 24: | ||
− | + | ==== Adding text ==== | |
>>> print("Brussels"+"Paris") | >>> print("Brussels"+"Paris") | ||
Line 33: | Line 33: | ||
− | + | ==== Composing a sentence ==== | |
>>> print("Paris", "to", "London", "via", "Brussels") | >>> print("Paris", "to", "London", "via", "Brussels") | ||
Line 42: | Line 42: | ||
− | + | ==== Multiply ==== | |
>>> print(3*3) | >>> print(3*3) | ||
Line 53: | Line 53: | ||
− | + | ==== Write a string as a variable ==== | |
** Avoids having to retype your string each time you use it | ** Avoids having to retype your string each time you use it | ||
Line 73: | Line 73: | ||
− | + | ==== Add punctuation ==== | |
>>> print(letter + " " + word + " " + sentence + ".") | >>> print(letter + " " + word + " " + sentence + ".") | ||
Line 86: | Line 86: | ||
− | + | ==== Calculate! ==== | |
** the length of the string | ** the length of the string | ||
Line 105: | Line 105: | ||
− | + | ==== What you've learned ==== | |
* variable | * variable | ||
Line 113: | Line 113: | ||
* integers | * integers | ||
* print() | * print() | ||
+ | * len() |
Revision as of 14:35, 21 November 2020
Next pages: Loops and Conditions // Create anthology
Contents
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 a string as a 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
Calculate!
- the length of the string
>>> print(len(letter)) >>> print(len(word)) >>> print(len(sentence)) >>> print(len(word))+2)
- and more
a_number = len(word)+2 print(a_number) a_number += 3 print(a_number)
- Exercise: Compose a sentence word by word, specifying each word as a variable. The length of the sentence is 20.
What you've learned
- variable
- value
- assignment operator (=)
- difference between variables and values
- integers
- print()
- len()