Day 3: learning python data variables and Data Types

Now variables are used to store values or reference the data in your program where the values can be accessed using the variable name.

gender = “man”
person_name = “Wapukha”
print(“hello denis, here is a story”)
print(“There was a great ” + gender  + ” called ” + person_name + “.”)
print(“this person was a talented and could do many things”)
print(person_name + ” could do many things such as sing and play soccer”)

Now the above is a story where gender and the name of the person are being assigned. It helps whenever the name of the person changes to another instead of changing it in every line of the content. Gender and person_name are variables. There are rules of naming variables where you do not start with a number or symbol.

Types of variables

Strings- in python strings are written in “” double quotes. Eg “man”, “wapukha” among others. There are different methods to present string in python. Through assignment to variable such as gender = “male” or triple quates can be used for multiple line of string eg.

gender = "man"
person_name = "Wapukha"
manylines = """ This is an example of many lines in the story
another line is here
third line comes here
there is another string of text on this line
"""
print("hello denis, here is a story")
print("There was a great " + gender  + " called " + person_name + ".")
print("this person was a talented and could do many things")
print(person_name + " could do many things such as sing and play soccer")
print(manylines)

 output

hello denis, here is a story

There was a great man called Wapukha.

this person was a talented and could do many things

Wapukha could do many things such as sing and play soccer

 This is an example of many lines in the story

another line is here

third line comes here

there is another string of text on this line

there are many functions that can be used with string to manipulate the content

here are some of the functions.

print(manylines.upper())

It is used to make the string upper case letters.

To get the length of the text you can use

print(len(manylines))

To get the number of characters.

There are many functions you can look at in pycharm

Numbers- they are written without the quotation marks such as 20 50, 53, also can be decimal numbers 456.8887543

Basics of numbers in python- they are commonly used data type

Print( 2.078) it prints on screen 2.078

Using print you can do all types of number operations such as pint (3* 4-5)

Mod which offers reminder can be computed in python print(20 % 3)

print(35%3)

Variables can be used to store the number e.g

My_num = 5

Print (My_num) then it will print on screen 5.

You can convert a number to a string using the str function e.g print(str(My_num)) which converts 5 to a string.

Other functions and how they are used

print(35%3)
print(pow(5, 4))
print(max(56, 32))
print(min(500, 234))
print(round(56.905))
output 
2
625
56
234
57

pow(x,y) gives x raised to y

note: to get more of the functions use the import math at the top of the program so that it can import more of the functions to the project. Type the line at top of the project

the max() functions returns the biggest number, the min() function returns smallest number, the round() rounds off the number to the nearest whole number.

More functions

 
print(floor(4.6))
print(ceil(4.6))
print(sqrt(49))
Output
4
5
7.0

Getting input from the user

To get the input in python the function input (), it will prompt the user to enter a value.

Example

Name= input(“Enter your Name:      ” )

Print (“hello ”  +  +  “! You are ” + age + )

Code example

name= input("Enter your name: ")
age= input("enter your age: ")
print("Hello "+ name + "! your are " + age + "  Year you can now order")
Output  
Enter your name: Denis
enter your age: 24
Hello Denis! your are 24  Year you can now order

Boolean- this is the true of false values. E.g is_male = false, if the person is male or not.

from math import *

from math import *

Developing a simple calculator with python

My first version of calculator

Building a basic calculator

The aim of the tutorial or my leaning journey will be to create a point of sale. Well I want it web based and this is the journey. I have to learn how to create a basic calculate. I will keep this code so that I can use later in my program

First I will create two variables that will store the variables.

num1 = input("enter a number: ")
num2 = input("enter second number: ")
result = float(num1) + float(num2)
print(result)
output 
enter a number: 5
enter second number: 7
12.0
 

The above calculator asks the user to enter the two numbers and it adds the two numbers and give the output. It means it’s a basic addition calculator.

CODE MODIFIED MORE TO GIVE BALANCE TO THE CUSTOMER

from math import *
num1 = input(“enter a number: “)
num2 = input(“enter second number: “)
result = float(num1) + float(num2)
print(result)
num3 = input(“enter amount tendered: “)
balance= float (num3)- result
print( “give customer: “)
print(balance)

OUTPUT

enter a number: 56

enter second number: 4

60.0

enter amount tendered: 100

give customer:

40.0

Adding more functions on a calculator

The assumption is that the purchase is made and the customers gives/tenders a note the system should indicate how much they should be given as change.

List in Python

Lists are important in python

The elements in the list are indexed starting from 0.

For instance a list of fruits.

Fruits= [ “Apple”, “ Mangoes”, “Kiwi”, “Orange” ]

Fruits= [“Apple”, “Mangoes”, “Kiwi”, “Oranges”,”Avacado”]
price= [5, 6, 5, 4, 8]
print(Fruits)
print(Fruits.extend(price))
print(Fruits)
print(Fruits.append(“Coconut”))
print(Fruits)
print(Fruits.insert(2, “Paw Paw”))
print(Fruits)
print(Fruits.clear())
print(Fruits)
print(price.sort())
print(price)
print(price.count(5))

Out put

[‘Apple’, ‘Mangoes’, ‘Kiwi’, ‘Oranges’, ‘Avacado’]

Printed out the list of

None

[‘Apple’, ‘Mangoes’, ‘Kiwi’, ‘Oranges’, ‘Avacado’, 5, 6, 5, 4, 8]

None

[‘Apple’, ‘Mangoes’, ‘Kiwi’, ‘Oranges’, ‘Avacado’, 5, 6, 5, 4, 8, ‘Coconut’]

None

[‘Apple’, ‘Mangoes’, ‘Paw Paw’, ‘Kiwi’, ‘Oranges’, ‘Avacado’, 5, 6, 5, 4, 8, ‘Coconut’]

None

[]

None

[4, 5, 5, 6, 8]

2

Leave a Reply

Your email address will not be published. Required fields are marked *