Python – Getting started (7 Part Series)
1 Python – First steps
2 Python – Data types
… 3 more parts…
3 Python – Variables
4 Python – Calculator sample
5 Python – Conditionals
6 Python – Functions and variable scopes
7 Python – Lists and loops
Contact
Variables
Computer programs use variables to hold and work with values in memory. For this reason it’s important to know a little bit more about them.
Keeping hard coded values
This sample shows how to print a calculated value to the user
<span>myNumber</span> <span>=</span> <span>2</span><span>print</span><span>(</span><span>myNumber</span><span>)</span><span>myNumber</span> <span>=</span> <span>2</span> <span>print</span><span>(</span><span>myNumber</span><span>)</span>myNumber = 2 print(myNumber)
Enter fullscreen mode Exit fullscreen mode
Getting value from user input
Most of the time, the program will wait or ask the user to input values. To do this with python is very simple. We’ll use input() function for that.
<span>myNumber</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter a number: </span><span>"</span><span>)</span><span>print</span><span>(</span><span>myNumber</span><span>)</span><span>myNumber</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter a number: </span><span>"</span><span>)</span> <span>print</span><span>(</span><span>myNumber</span><span>)</span>myNumber = input("Enter a number: ") print(myNumber)
Enter fullscreen mode Exit fullscreen mode
Breaking line
Using this code below, the program will wait for a value from the user.
<span>myName</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter your name</span><span>"</span><span>)</span><span>print</span><span>(</span><span>myName</span><span>)</span><span>myName</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter your name</span><span>"</span><span>)</span> <span>print</span><span>(</span><span>myName</span><span>)</span>myName = input("Enter your name") print(myName)
Enter fullscreen mode Exit fullscreen mode
The issue here is that the user’s response will be concatenated directly with the question.
To solve that, we can use escape characters to insert a new line \n.
<span>myName</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter your name</span><span>\n</span><span>"</span><span>)</span><span>print</span><span>(</span><span>myName</span><span>)</span><span>myName</span> <span>=</span> <span>input</span><span>(</span><span>"</span><span>Enter your name</span><span>\n</span><span>"</span><span>)</span> <span>print</span><span>(</span><span>myName</span><span>)</span>myName = input("Enter your name\n") print(myName)
Enter fullscreen mode Exit fullscreen mode
Now, the question will be showed in one line and the answer in another one.
Notes
You can access this code on github.
Typos or suggestions?
If you’ve found a typo, a sentence that could be improved or anything else that should be updated on this blog post, you can access it through a git repository and make a pull request. If you feel comfortable with github, instead of posting a comment, please go directly to https://github.com/campelo/documentation and open a new pull request with your changes.
Python – Getting started (7 Part Series)
1 Python – First steps
2 Python – Data types
… 3 more parts…
3 Python – Variables
4 Python – Calculator sample
5 Python – Conditionals
6 Python – Functions and variable scopes
7 Python – Lists and loops
原文链接:Python – Variables
暂无评论内容