Baby Python Basic Code For Programming Adrian Wallen
Baby Python Basic Code For Programming Adrian Wallen
Hello World
***********************
Next on our learning agenda is how to combine strings.
[Input]
[Output]
I love you
************************
[Input]
first= "I"
second= "love"
third= "Monty Python"
sentence= first + ' ' + second + ' ' + third + '. '
print (sentence)
[Output]
**************************
I order to combine both a string and a number together, you first have to turn the number into a string.
[Input]
version = 3
print ('I love Monty' + str(version) + '. ')
[Output]
I love Monty3.
***************************
There are times you will have to act interactively with the computer,
entering information when the computer asks you for it, like in polls. Tell it you are going to vote
Democrat. (No, not really :)
[Input]
[Output]
Hello Fred
*****************************
[Input]
[Output]
*******************************
Now is a good time to talk about Operators. They are:
== equal to
> greater than
>= equal or greater
< less than
<= equal or less than
!= not equal
<> another way of saying not equal
********************************
Let us also bring in arithmetic operators at this time. The order of math is from left to right. With the
exception of exponents which go right to left. Here they are, in order of how they are seen by the
computer, first performed at the top:
( ) brackets
** exponents
* multiplication
/ division
% modulus [It returns the remainder]
// floor [It removes the remainder]
+ addition
- subtraction
**********************************
[Input]
[Output]
----------
***********************************
[Input]
weirdness = 'weird' * 3
print (weirdness)
[Output]
weirdweirdweird
*************************************
Example of how Modulus % works
[Input]
17%5
[Output]
**************************************
Example of Floor //
[Input]
17//5
[Output]
****************************************
Functions such as Length, Uppercase, Range, Count, Content check, Sorting, Max:
[Input]
max(3, 17, 5, 0)
[Output]
17
*****************************************
Content Check
a = [1,2,3,4,'a','b','c']
return 'a' in a
if 'c' in 'Python':
print 'YES'
else:
print 'NO'
******************************************
Count Function
[Input]
[Output]
4
*******************************************
Sorting Alphabetically Function
[Input]
mylist=['charlie' , 'able' , 'baker']
mylist.sort( )
mylist
[Output]
*******************************************
Sorting Numerically
[Input]
mylist = [1, 3, 2, 4, 5]
mynewlist= sorted (mylist)
mynewlist
[Output]
[1, 2, 3, 4, 5]
********************************************
[Input]
fruit = 'apple'
print (fruit.upper( ))
[Output]
APPLE
********************************************
Range Function
[Input]
for i in range(5):
print(i)
[output]
0
1
2
3
4
Here I might point out that when Python counts a list, the first item in the list is given the numerical
value zero. And pay attention to how print(i), above, had to be indented, about four spaces in. You will
see indents like this in all loops.
**********************************************
Length Function
[Input]
fruit = 'apple'
print (len(fruit) )
[Output]
5
[apple has 5 characters]
**********************************************
Writing to a file. Maybe there is a file you want to write stuff to.
[Input]
***********************************************
Maybe there is a a file you just want to read.
[Input]
data= myfile.read ( )
***********************************************
Tuple Assignment. A Tuple is a fixed list.
[Input]
print (mon)
print (fri)
[Output]
Monday
Friday
**************************************************
The difference between While and If Else loops is
that While asks questions many times over ...
If Else just asks a question once.
n = 100
s=0
while s<n:
s=s+1
print (s, "bottles of beer")
[output ]
1 bottles of beer...
.............................
100 bottles of beer
***************************************************
[Input]
i=1
s = 18
while (s > 17) and (s < 21):
[ Output]
you need a license
you need a license
you need a license
***************************************************
If Else Statements
[Input]
n = 21
if (n < 18) or (n >65):
print ("no license needed")
else:
print ("license needed")
[Output]
license needed
****************************************************
[Input]
mark = 78
print ("A")
else:
if mark >= 80:
print ("B")
else:
if mark >= 70:
print ("C")
else:
if mark >= 60:
print ("D")
else:
print ("F")
[output]
C
*****************************************************
Here is the previous If Else program in Elif format:
[Input]
mark = 78
else:
print ("F")
[output]
C
******************************************************
How to download the Interpreter:
For PC's go to
https://www. python.org/downloads/
for macs go to
https://www. python.org/downloads/mac-osx
When the download completes, double click on the .exe file to install it.
I went to the apple store and downloaded the app for my ipad: pythoni 3.3
Works great.
PS: All the code here was run on pythoni 3.3 to make sure they are accurate.
******************************************************
Other crazy sixties comedies that you might watch and base a whole
flippin code on:
The Fearless Vampire Killers (In Romania, Jewish vampires not held off by crosses)
Casino Royale (everybody is James Bond, even the dog)
What did you do in the War, daddy? (When people get drunk in Italy, it's make love not war)
What's New, Pussycat? (20 crazy people run wild across France...they can take it)
Those Magnificent Men in Their Flying Machines (Wright Brothers, move over)
The Great Race (American Victorian Feminist races with Dudley Do-right and Doc Doom)
Barbarella (In the year 5,000 A.D. Jane Fonda takes a space walk in the buff.)
It's a Mad Mad World (Bonzo Californians race to Santa Rosita wrecking everything and the police are
told not to interfere)