Actions

Python for beginners: Difference between revisions

From Algolit

(LETTER * WORD * SENTENCE)
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Literary Python for Beginners ==
+
Next pages: [[Python_for_beginners/collections|Collections]] // [[Python_for_beginners/loops_and_conditions|Loops and Conditions]] // [[Python_for_beginners/anthology|Create anthology]]
  
=== LETTER * WORD * SENTENCE ===
+
'''=== VARIABLES ==='''
  
 
* 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
  
  
** run python 2.7 – installed by default on all platforms
+
'''# USING STRINGS'''
  
$ python
+
A string is a chain of characters / text and can contain any type of characters
  
>>>
+
A string is defined by " "
  
** install python idle 2.7 – ways to interact with Python: shell + editor
 
  
 +
'''* Write text using STRING'''
  
 +
>>> print("La Cambre")
  
'''# USING STRINGS'''
+
** Exercise: Write your name'''
 
 
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"
+
'''* Adding text'''
  
'''* write several letters'''
+
>>> print("Brussels"+"Paris")
  
>>> "a, b, c, d"  
+
>>> print("Brussels "+"Paris")
  
'''* write combinations'''  
+
** Exercise: Write your address'''
  
>>> "a" + "b"
 
  
>>> 3
+
'''* Composing a sentence'''
  
>>> "a" + "and" + "b"  
+
>>> print("Paris", "to", "London", "via", "Brussels")
  
>>> "a" + " and " + "b"  
+
>>> print("Paris to London via Brussels")
  
>>> "a" + "+ " + 3 + " is a3"  ***
+
** Exercise: Write your favourite expression'''
  
>>> 3*3
 
  
>>> "a" + "+ " + "3" + " is a3"
+
'''* Multiply'''
  
>>> 3 * "algolit" + " in wtc25"
+
>>> print(3*3)
  
 +
>>> 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 string as 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 62: Line 59:
 
>>> 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
  
  
'''* 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 88: Line 85:
  
  
'''* calculate!'''
+
'''* Calculate!'''
  
 
** the length of the string
 
** the length of the string
  
>>> len(letter)  
+
>>> print(len(letter))
>>> len(word)  
+
>>> print(len(word))
>>> len(sentence)  
+
>>> print(len(sentence))
 
+
>>> print(len(word))+2)
 
 
** 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()
+
** and more
  
 +
a_number = len(word)+2
 +
print(a_number)
 +
a_number += 3
 +
print(a_number)
  
[[Some_Python_Vocabulary]]
+
** Exercise: Compose a sentence word by word, specifying each word as a variable. The length of the sentence is 20.
  
[[Loops_and_conditions]]
 
  
[[Anthology]]
+
'''What you've learned'''
 +
 
 +
* variable
 +
* value
 +
* assignment operator (=)
 +
* difference between variables and values
 +
* integers
 +
* print()

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