Introduction to Python

Download Thonny IDE from:

https://thonny.org/

Thonny is an Integrated Development Environment for beginners. Yet it is powerful enough to build robust applications.

Input a value

Open Thonny and write your first program:

name = input ("what is your name?")
print ("Hello", name)

You may use double or single quotation marks (‘ or ”) to print a message to the screen. The input() command will accept the input from the user and store it to a variable. Inside the parenthesis we can prompt a message to the user.

In the previous example we used a variable named ‘name’.

The variable is a position in computer’s memory where we can hold (and change) a value. For more on variables you can see the Variables post.

In Python, variable names can contain only letters, numbers, and underscores. A variable can start with a letter or an underscore, but not with a number.

In our example, the value in variable name is a string, i.e. a set of characters.

We may use int(input()) to input an integer (int):

name = input ("what is your name?")
age = int(input("What is your age?"))  
print ("Hello", name, "you are", age, "years old.")

We can prompt the same messages for the inputs with a “\n”. In Python we use backslash (\) to represent certain whitespace characters (n for new line or t for tab).

In Python 3 we can use the “f” character to declare a formatted output for the print() command. In the following statement we use a formatted output with the variables inside brackets { }.

print (f"Hello {name}, you are {age} years old.")

We can also perform some calculations. The following line will produce 3.0 (float number) as a result.

print ((10-2)*3/8)
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.