Innings2
Powered by Innings 2

Glossary

Select one of the keywords on the left…

Python Advanced > Basics

Basics

This is an interactive version of the official Python tutorial from "A Byte of Python" by Swaroop C H. Original book.

This is an adapted version of the book. This version is not from the original author of this book

The original text can be fetched from this location.

Just printing 'hello world' is not enough, is it? You want to do more than that - you want to take some input, manipulate it and get something out of it. We can achieve this in Python using constants and variables, and we'll learn some other concepts as well in this chapter.

Try your first Python command: print('hello')

print('')

Comments

Comments are any text to the right of the # symbol and is mainly useful as notes for the reader of the program.

For example:

print('hello world') # Note that print is a function

or:

# Note that print is a function
print('hello world')

Use as many useful comments as you can in your program to:

  • explain assumptions
  • explain important decisions
  • explain important details
  • explain problems you're trying to solve
  • explain problems you're trying to overcome in your program, etc.

Code tells you how, comments should tell you why

This is useful for readers of your program so that they can easily understand what the program is doing. Remember, that person can be yourself after six months!

What's the correct symbol for writing comments in Python?

Practice: Add a comment to explain this code:

print('Hello') This prints a greeting

Literal Constants

An example of a literal constant is a number like 5, 1.23, or a string like 'This is a string' or "It's a string!".

It is called a literal because it is literal - you use its value literally. The number 2 always represents itself and nothing else - it is a constant because its value cannot be changed. Hence, all these are referred to as literal constants.

Which of these is NOT a literal constant?

Let's categorize these literals:

42
-17
3.14
"Hello"
'Python'
Integer Constants
Float Constants
String Constants

Numbers

Numbers are mainly of two types - integers and floats.

An example of an integer is 2 which is just a whole number.

Examples of floating point numbers (or floats for short) are 3.23 and 52.3E4. The E notation indicates powers of 10. In this case, 52.3E4 means 52.3 * 10^-4.

Note for Experienced Programmers

There is no separate long type. The int type can be an integer of any size.

Write 1000 in scientific notation using Python syntax: