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 functionor:
# 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')
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
Which of these is NOT a literal constant?
Let's categorize these literals:
Numbers
Numbers are mainly of two types - integers and floats.
An example of an integer is
Examples of floating point numbers (or floats for short) are
Note for Experienced Programmers
There is no separate
Write 1000 in scientific notation using Python syntax: