Learn Python 1: Variables, input and print


In Python, variables are simply names given to values. For instance, if there is a number, say 3.14159, and I'd want to use it frequently without having to type it every time, I can use a variable to assign a name to it and solve this issue.

pi = 3.14159

The value of pi is stored in the variable "pi" as 3.14159, and it holds the same value when used.
Let's create a program that takes the radius of a circle as an input and calculates its circumference as an output.

pi = 3.14159
radius = float(input("input radius: "))
c = 2*pi*radius
print(c)

 

In line one, we declare a variable called "pi". On line two, we use the "input()" function to take input from the user. This function returns the input value, which we store in the "radius" variable. However, before we can do that, we need to convert the input value to a float, which is a number that includes a decimal point. In line 3, we use the formula 2*pi*r to calculate the circumference, which is stored in variable c, and finally in line 4, we print the variable to the console using the "print()" function. To test this code, you can go to https://www.online-python.com/ and write the above code in the main.py file that is there by default. Understanding how variables work in Python is fundamental to writing efficient and effective code. Experimenting with programs like this helps solidify your understanding and prepares you for more complex coding tasks in the future.

Keep exploring and happy coding!

How do you rate this article?

2


gittygiddy
gittygiddy

i love cats


Learning Python: A to Z blog
Learning Python: A to Z blog

Python is a powerful programming language used in web development, data science, machine learning, and artificial intelligence. Our community offers comprehensive learning, with experienced developers guiding learners. Join us to collaborate on exciting projects and unlock your full potential as a programmer.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.