Python Day 2 – Identifiers, Keywords and a First Look at Datatypes
Day 2 was about naming things, the words Python keeps for itself, and what kind of data we can store.
Identifiers (aka variables)
A name in a Python program is called an identifier or variables.
name = 'sanjay' doubt = True questions = 5 Rules:
- Letters (upper or lower case) are allowed
- Digits are allowed, but not at the start (
1nameis invalid) - Underscore
_is allowed
Underscores also carry a meaning by convention:
-
_name→ treat it as private -
__name→ strongly private -
__name__→ special, reserved for Python's own use
Python doesn't actually enforce private like Java does. It just trusts you. That ties into the Zen of Python (try import this). My favourite line: "If the implementation is hard to explain, it's a bad idea."
And = is the assignment operator. It takes the value on the right and gives it to the name on the left.
Reserved words (keywords)
Some words already mean something to Python, so you can't use them as variable names. You can list them like this:
import keyword print(keyword.kwlist) print(len(keyword.kwlist)) You'll see things like if, else, while, for, def, return, True, False, None.
IDE or not?
Our trainer said beginners should skip heavy IDEs like PyCharm for now and just use IDLE or a simple editor. On servers you usually won't have an IDE anyway. Fair point.
Datatypes
Everything a computer stores is binary in the end. Even the letter 'a' is a number underneath:
bin()
print(ord('a')) # 97 print(bin(ord('a'))) # 0b1100001 The basic types:
- int: -3, 0, 42
- float: 0.2, -1.5
- complex: 3+4j
- str: text
- bool: True / False