Python Tutorial for Beginners - #2 Variables & Data Types
Variables
Hi, SteemIt community. In this lecture we’re going to learn variables in Python. Variable is like a container where you can store various data types in Python. For intance, this program;
print(“Hello, SteemIt!”)
doesn’t have any variables. If we execute this code in Python Interactive Shell, prints out to screen “Hello, SteemIt!” string. String is a data type. We will cover it later don’t worry. If we want to store “Hello, SteemIt!” string, we declaring the string to a variable. You’ll store values in variables with an assignment statement. An assignment expressions includes of a variable name, an equal sign (called the assignment operator if we used double equal sign then called comparison operator), and the value to be stored.
greeting = “Heelo, SteemIt!”
print(greeting)
You can name a variable anything as long as it obeys the following three rules:
- It can be only one word.
- It can use only letters, numbers, and the underscore ( _) character.
- It can’t begin with a number.
Valid Variable Names | Invalid Variable Names |
---|---|
point | total-points (hypens are not accepted) |
totalPoints | total point (spaces are not accepted) |
user2 | 2user (can’t begin with a number) |
steemIt | $teemIt (special characters like $ are not accepted) |
Variable names are case-sensitive, meaning that steem, Steem, STEEM, StEEm and sTeeM are five different variables.
Python reads and executes scripts from top to bottom. If we declare same variable multiple times, variable will be updated in every line.
a = 7
a = 9
a = 11
print(a + a + a + a)
Python prints out 44 (11 + 11 + 11)
Data Types
Data Types | Example |
---|---|
Integers | -15, -13, -12, 0, 5, 18 |
Floating Point Number | -15.5, -10.0, -3.29, 0.0, 3.75, 4.65 |
Strings | ‘s’, ‘ss’, ‘steem’ ‘Hello!’, ‘7’, ‘2 Steemians’ |
Always surround your string in single quote (') or double quotes (‘) characters (as in 'Hello, SteemIt' or “Hello, Steemians”) so Python knows where the string begins and ends. If you ever see the error message SyntaxError: EOL while scanning string literal, you probably forgot the final single quote or double quote character at the end of the string, like in this example:
>>> 'Hello world!
SyntaxError: EOL while scanning string literal
Next section we will explain data types in detail.
Congratulations @scaevola! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
You got your First payout
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP