Programming For Adults
Programming For Adults
1) Built-in features
Calculator >>>5+3 >>>5-3
8 2
>>>5*3 >>>5**3
15 125
>>>5/3 >>>5//3
1.6666666666666667 1
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)
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.
>>>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
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