Basic Code Examples – Python Codes

Basic Code Examples – Python Codes

Printing:

Print is a function that will print whatever you want onto the screen. For example:

print(“Hello everyone”)

This prints Hello everyone to the screen.

Input:

Input is a function which is used to ask the user something or get something from the user. For example, if i wanted to know someone’s name:

name = input(“what is your name: “)

You can make name anything you want. It doesn’t have to be called name, although it is easiest to name the variable something that is related to it. The variable is whatever you are making the input equal. In this case name is the variable.

Integer Inputs:

Say I wanted to ask for someone’s age, I would write:

age = int(input(“what is your age: “))

We write int because age is a number, therefore is an integer.

Printing Inputs:

If I wanted to print the name:

name = input(“what is your name: “)
print(name)

This prints to the screen whatever the user entered as their name. To print an integer, like age:

age = int(input(“how old are you? “))
print(age)

Numeracy:

We can use python to add, multiply, divide, subtract integers, but first we need to get the numbers:

num1 = int(input(“enter a number: “))
num2 = int(input(“enter a number: “))

Now we can add them together:

totalSum = (num1) + (num2)
print(totalSum)

This is the same with subtraction, division and multiplication, we just simply swap the + with – (subtraction), / (division), * (multiplication).

Printing Multiple Inputs in one Line:

If we wanted to print multiple inputs in a sentence, we would use this:

name = input(“what is your name: “)
age = int(input(“how old are you? “))
print(“hello ” + (name) + “, you are ” + str(age) + ” years old!”)

via Python Codes

March 30, 2019 at 08:13AM