Adam El M’Rabet IMSAL coding events 09/05/2018
IMSAL coding events for adults:
Learning Python (session 1)
• Python: is a (interpreted) high level programming language
• PyCharm: is an IDE (Integrated Development Environment) for the Python language.
So PyCharm is only a coding assistance, not a language. It provides features like code-
completon, quick fxes, error highlightng etc. (PyCharm itself is writen in Java & python).
Content of this session
1. Some built-in features of Python
1. Built-in calculator
2. Built-in datatypes (int, foat, str )
3. Built-in functons (input(), print())
4. How to make comments by using ‘#’
2. Variables
1. How to initalize a variable (by giving it a value)
2. How to modify the content of a variable
3. How to make mathematcal operatons with variables
4. How to name variables (multple words seperated by underscore (‘_’) not by a space !)
3. Strings
1. A string is an array of characters
2. How make indexaton and take slices of a string by using []
4. Applying the built-in functons to ask for user input and “outputngn a welcome message
5. Conditonals
1. Boolean datatype (True, False)
2. If, else.
3. Boolean operators (and, or)
6. Applying conditonals for making a user-login program (based on name and password)
Adam El M’Rabet IMSAL coding events 09/05/2018
1) Built-in features
Calculator >>>5+3 >>>5-3
8 2
>>>5*3 >>>5**3
15 125
>>>5/3 >>>5//3
1.6666666666666667 1
Datatype >>>type(2) >>>type(2.0)
int float
Ask for Input input() # When the compiler will reach this line, it
# will stop here and wait for some input (that
# finishes when hitting enter)
input("What is your name? ") # This will print out
# “What is your name? ”
# and wait for some input
Print on standard print(2) # → 2 print(2+2) # → 4
output stream print("Hello world !") # → Hello world
https://www.youtube.com/watch?v=hnxIRVZ0EyU&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_&index=1
2) Variables
Initializing and >>>a = 2 # Always: variable-name at the LEFT gets
modifying >>>b = 3 # value at the RIGHT.
>>>a = b # It is ‘a’ that gets the value of ‘b’.
# ‘b’ stays unchanged !! (a=3,b=3)
>>>my_name = "Adam"
Adam El M’Rabet IMSAL coding events 09/05/2018
3) Strings
Indexing >>>my_word = "IMSAL"
>>>my_word[0] # Get first character (starts with 0)
'I'
>>>my_word[1] # Get second char
'M'
>>>my_word[-1] # Get last char
'L'
>>>len(my_word) # Get length of the string
5
Slicing >>>my_sentence = "IMSAL is cool" #Space counts as char
>>>my_sentence[6:8] # From pos 6 to 8(8 non included)
'is'
>>>my_sentence[5:6]
' ' # → empty string
>>>my_sentence[:5] # [:5] is same as [0:5]
'IMSAL'
>>>my_sentence[6:] # [6:] is same as [6:end]
'is cool'
>>>my_sentence[:] # [:] is same as [0:end]
'IMSAL is cool'
https://www.youtube.com/watch?v=nefopNkZmB4&index=2&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_
https://www.youtube.com/watch?v=YbipxqSKx-E&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_&index=3
4) Welcome-message program
name = input("Hello, what is your name ? ")
print("Welcome", name)
/usr/bin/python3.6 /home/adam/Documents/IMSAL/CodingSessions/File1.py
Hello, what is your name ? Adam
Welcome Adam
Process finished with exit code 0
Adam El M’Rabet IMSAL coding events 09/05/2018
5) Conditionals
Boolean operations >>>True and False >>>True and True >>>True or False
False True True
Comparison >>>a = 2 >>>b = 3
>>>a == b # Is a equal to b ?
False
>>>a != b # Is a not equal to b ?
True
>>>a < b >>>a >= b
True False
Conditionals a = 2
if a == 2:
print("something")
else:
print("something else")
https://www.youtube.com/watch?v=bk22K1m0890&list=PL6gx4Cwl9DGAcbMi1sH6oAMk4JHw91mC_&index=6
6) User-login program
name = input("name: ")
password = input("password: ")
if name == "Adam" and password == "mypassword":
print("Welcome sir", name)
else:
print("Invalid name and/or password")
/usr/bin/python3.6 /home/adam/Documents/IMSAL/CodingSessions/File1.py
name: Adam
password: mypassword
Welcome sir Adam
Process finished with exit code 0