Actions

Difference between revisions of "Python for beginners"

From Algolit

 
Line 1: Line 1:
 +
Next pages: [[Python_for_beginners/collections|Collections]] // [[Python_for_beginners/loops_and_conditions|Loops and Conditions]] // [[Python_for_beginners/anthology|Create anthology]]
  
Next pages: [[Python_for_beginners/loops_and_conditions|Loops and Conditions]] // [[Python_for_beginners/anthology|Create anthology]]
+
'''=== VARIABLES ==='''
 
 
 
 
=== VARIABLES ===
 
  
 
* Introduction to the objects string & list with their different attributes
 
* Introduction to the objects string & list with their different attributes
* use of the shell
+
* uses the shell
  
  
Line 16: Line 14:
  
  
==== Write text using STRING ====
+
'''* Write text using STRING'''
  
 
>>> print("La Cambre")  
 
>>> print("La Cambre")  
  
* Exercise: Write your name'''
+
** Exercise: Write your name'''
 +
 
 +
>>> ...
  
  
==== Adding text ====
+
'''* Adding text'''
  
 
>>> print("Brussels"+"Paris")
 
>>> print("Brussels"+"Paris")
Line 29: Line 29:
 
>>> print("Brussels "+"Paris")  
 
>>> print("Brussels "+"Paris")  
  
* Exercise: Write your address'''
+
** Exercise: Write your address'''
  
  
==== Composing a sentence ====
+
'''* Composing a sentence'''
  
 
>>> print("Paris", "to", "London", "via", "Brussels")
 
>>> print("Paris", "to", "London", "via", "Brussels")
Line 38: Line 38:
 
>>> print("Paris to London via Brussels")
 
>>> print("Paris to London via Brussels")
  
* Exercise: Write your favourite expression'''
+
** Exercise: Write your favourite expression'''
  
  
==== Multiply ====
+
'''* Multiply'''
  
 
>>> print(3*3)
 
>>> print(3*3)
Line 47: Line 47:
 
>>> 3 * "algolit" + " in Brussels"  
 
>>> 3 * "algolit" + " in Brussels"  
  
* Exercise: Write 'I write the alphabet' 3 times.
+
** Exercise: Write 'I write the alphabet' 3 times.
  
 
Note: there are always different possible solutions  
 
Note: there are always different possible solutions  
  
  
==== Write a string as a variable ====
+
'''* 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 69: Line 69:
 
>>> print(sentence, letter)
 
>>> print(sentence, letter)
  
* Exercise: Print your letter, word, sentence
+
** Exercise: Print your letter, word, sentence
  
  
==== Add punctuation ====
+
'''* Add punctuation'''
  
 
>>> print(letter + " " + word + " " + sentence + ".")  
 
>>> print(letter + " " + word + " " + sentence + ".")  
Line 82: Line 82:
 
>>> print letter + "! " + word + "? " + sentence + "."  
 
>>> print letter + "! " + word + "? " + sentence + "."  
  
* Exercise: change content of one of variables, over and over, see how result changes
+
** Exercise: change content of one of variables, over and over, see how result changes
  
  
==== Calculate! ====
+
'''* Calculate!'''
  
 
** the length of the string
 
** the length of the string
  
 
>>> print(len(letter))
 
>>> print(len(letter))
 
 
>>> print(len(word))
 
>>> print(len(word))
 
+
>>> print(len(sentence))  
>>> print(len(sentence))
 
 
 
>>> print(len(word))+2)
 
>>> print(len(word))+2)
  
Line 100: Line 97:
  
 
a_number = len(word)+2
 
a_number = len(word)+2
 
 
print(a_number)
 
print(a_number)
 
 
a_number += 3
 
a_number += 3
 
 
print(a_number)
 
print(a_number)
  
* Exercise: Compose a sentence word by word, specifying each word as a variable. The length of the sentence is 20.
+
** Exercise: Compose a sentence word by word, specifying each word as a variable. The length of the sentence is 20.
  
  
==== What you've learned ====
+
'''What you've learned'''
 
    
 
    
 
* variable
 
* variable
Line 118: Line 112:
 
* integers
 
* integers
 
* print()
 
* print()
* len()
 

Latest revision as of 15:04, 21 November 2020

Next pages: Collections // Loops and Conditions // Create anthology

=== VARIABLES ===

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


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