Basics of python programming language
Input, comments, printing and Variable operators
Definition of terms
- Python - Is a programming language that is used for data science.
- Code - Is a set of instructions that a programmer writes so as to perform a particular task.
1. Printing
Example 1
print("I have just started python classes")
By printing we mean outputting our code to the console. As we carry on our studies we will get to learn the keywords which have two brackets as print() are called functions.
Practice challenge
Let's print "I am turning 25 years old."
2. Comments
They are used by programmers and data scientists to explain what their codes means. Usually this comments cannot be translated by the computer.
Example 1
The code below will print ("Hello world !").
# print("Hello world !")
Example 2
lets uncomment the above code
print("Hello world !")
Practice challenge 1
Print your home time
Practice challenge 2
a = I live with my aunt
3. variables
A variable is a container that we can use to store a value . This value can be a number, a character, a string, a list , a dictionary etc...
Example
So as to create a variable we use the equals sign; =
x = 3
Because we have assigned the values 3 to x
We now need to print out and see whether x has that value.
Then, print out x below this line using the print() function below.
print(x)
Practice challenge
Assign Guardian angel to the variable gospel music artists.
4. Operators
Example
We are going to use the variables a and b to perform some arithmetic operations. Let's run this cell once then.
print(a + b)
# print(a - b)
# print(a/b)
Uncomment the lines above to see the effect for multiplication, division and subtraction.
Practice challenge 1
print(a - b)
print(a * b)
print(a/b)
Practice challenge 2
Get the sum of two numbers from a user's input
Practice challenge 3
While paying for her parking fee of kshs 50, Jane inserts a note into the paying machine and gets back her change. Write a program that reads as input the note that is greater than kshs.200. and returns a change in notes of 100 and 50 . Use the knowledge of variables, comments and operators and input print the number of denominations given.
# hint
def no_notes(a);
q={500,200,100,50}
for i in a range (4);
q =q(i)
x + = int(a/q)
a = int(a%q)
if a>0;
x =-1
print(notes 100)
print(notes 50)
Python conditional (IF) statements
For one to write useful python programs, we always need to check the conditions and apply a certain operation accordingly. Conditional statements like the if statement provide us with that ability.
Example 1
We can write an if statement by using the if keyword as shown.
x = 300
y = 500
if y > x
print("y is greater than x")
Example 2
The elf keyword is a keyword that will try another condition if the previous condition was not true.
x = 33
y = 33
if y < x:
print("y is less than x")
elf x == y
print("x and y are equal")
Practice challenge
Write a program that reads an input from a user . If the user enters a , e, i , o, u then our program should display a message indicating that the entered letter is a vowel.
Example 3
The else keyword will catch anything else which isn't caught by the preceding conditions.
First, we declare variables.
if y > x:
print ("y is greater than x")
elf x==y:
print ("x and y are equal")
else
print("x is greater than y")
Practice challenge 1
Write a program that reads a wavelength from the user and it's color. Display an appropriate error message if the wavelength entered by the user is outside of the visible spectrum. You should use else keyword.
Practice challenge 2
Let's create a program that reads the name of a month from the user as a string, then displays the number of days in that month. The length of a month needs to also display 28 to 31 days. The program needs to also display 28 to 29 days for February. So that leap years are taken into account . The program should at least use the else keyword.
Practice challenge 3
Given the 3 sides of triangle x ,y and z. Find out whether the triangle is equilateral, isosceles or obtuse. N
Note, An equilateral triangle has all sides equal, isosceles means that two of the sides are equal not the third one, obtuse means all 3 are different . The user will be prompted to provide the values of x, y and z. The program should use AND keyword.
An example on OR variable
First we declare our variables
x = int (input("Enter the values of x ="))
y = int (input("Enter the values of y ="))
if ( x>=15) or (y<=25)
print ("x >= 15 or y <= 25 so if statement is True")
else:
print(" values of x < 15 and y > 25 so if statements is False !")
Continue......
Top comments (0)