This little corner is for my children. Have fun guys!
Enter Raspberry PI
First Step in Python
Variables
Taking Input
What if
Lather, Rinse, Repeat
Dissecting a Loop
Coding Warm Up 1
Raspberry Pi |
.
Raspberry Pi |
pi@tron ~ $ python3 even-odd.py
Please enter a number: 5
The number, 5 is ODD.
Nice talking to you. Bye!
pi@tron ~ $ python3 even-odd.py
Please enter a number: 4
The number, 4 is EVEN.
Nice talking to you. Bye!
pi@tron ~ $
number = int(input("Please enter a number: "))
if number % 2 == 0:
print("The number,", number, "is EVEN.")
else:
print("The number,", number, "is ODD.")
print("Nice talking to you. Bye!")
for n in range(1, 21):
print(n, "squared is", n * n)
pi@tron ~ $ python3 loop-again.py
Using variable i in the loop.
Let's go!
Iteration 0 - variable i = 0
Iteration 1 - variable i = 1
Iteration 2 - variable i = 2
Iteration 3 - variable i = 3
Iteration 4 - variable i = 4
Iteration 5 - variable i = 5
Program terminates! Bye!
pi@tron ~ $
pi@tron ~ $ python3
>>> numbers = list(range(1,10))
>>> print(numbers)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
>>> butterflies = list(range(11))
>>> print(butterflies)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
>>> even = list(range(2,11,2))
>>> print(even)
[2, 4, 6, 8, 10]
>>>
print("**********************************")
print("*** Program Calculating Square ***")
print("*** of the first 20 numbers ***")
print("**********************************")
for n in range(1, 21):
print(n, "squared is", n * n)
pi@tron ~ $ python3 loop.py
**********************************
*** Program Calculating Square ***
*** of the first 20 numbers ***
**********************************
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
6 squared is 36
7 squared is 49
8 squared is 64
9 squared is 81
10 squared is 100
11 squared is 121
12 squared is 144
13 squared is 169
14 squared is 196
15 squared is 225
16 squared is 256
17 squared is 289
18 squared is 324
19 squared is 361
20 squared is 400
pi@tron ~ $
pi@tron ~ $ python3 loop-name.py
What is your name?
Neila
Enter a number (not to big please!):
4
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
Hello Neila I am Raspberry PI and I love working with you!
pi@tron ~ $
pi@tron ~ $ python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>&g 'python' == 'python' True >>
>
'python' == 'Python' False >>
>
8 < 100 True >>
>
'car' != 'bike' True
print("What is your name?")
name = input()
if name == 'Natalie':
print("Hello, Natalie! It's so good to see you again!")
else:
print("Nice to meet you", name, "!")
pi@tron ~ $ python3 if.py
What is your name?
Jimmy
Nice to meet you Jimmy !
pi@tron ~ $
pi@tron ~ $ python3 if.py
What is your name?
Natalie
Hello, Natalie! It's so good to see you again!
pi@tron ~ $
i = 8;
if i > 8:
print("Variable i is greater than 8.")
print("These two print statements won't be printed!")
else:
print("Variable i is not greater than 8.")
print("Program ends!")
pi@tron ~ $ python3 if-statement.py
Variable i is not greater than 8.
Program ends!
pi@tron ~ $
i == 8 # true if i is equal to 8.
i != 8 # true if i is not equal to 8.
i > 8 # true if i is greater than 8.
i > 8 # true if i is less than 8.
i <= 8 # true if i is less or equal to 8.
i >= 8 # true if i is greater or equal to 8.
In a nutshell if the condition is true, the block of code will run
Else
We run different block of code.
print("*******************************")
print("*** Welcome to Python Club ***")
print("*******************************")
print("What is your name?")
name = input()
print("How old are you?")
age = int(input())
if age < 9:
print("We love young adventurers in our Python club!")
elif age < 16:
print("Welcome young apprentice. You're goint to love Python!")
else:
print("Python will be your second nature in no time!")