Python Basics
Print Function
print(): Function to print the output to the console.type(): Function to print the type of the variableexit(): Function to exit the program
Comments
#: Used for single-line comments.Ctrl + /: Shortcut to comment selected lines.
Data Types
1. Text Data Type
- String: A collection of characters represented using single quotes or double quotes.
2. Numeric Data Type
- Integer: Numbers without decimal points.
- Float: Numbers with decimal points.
- Complex: Numbers in the format
a + bj, whereais the real part andbis the imaginary part.
3. Sequential Data Type
- List:
- Collection of elements (can be called an array).
- Ordered collection.
- Can contain any data type.
- Mutable (changeable).
- Indexed (first index position is 0).
-
Defined by
[]. -
Tuple:
- Collection of elements similar to a list.
- Ordered collection.
- Indexed (first index position is 0).
- Immutable (not changeable).
- Defined by
().
4. Mapping Data Type
- Dictionary:
- Collection of key-value pairs.
- Not indexed.
- Duplication of keys is not allowed.
- Mutable.
- Defined by
{}.
5. Set Data Type
- Set:
- Collection of unique elements.
- Not indexed.
- Duplication of elements is not allowed.
- Unordered.
- Defined by
{}.
6. Boolean Data Type
- Boolean: Represents
TrueorFalse.
Variables
A Python variable is a reserved memory location to store values. In other words, variables in a Python program give data to the computer for processing.
Rules for Python Variables
- Variables start with a letter or underscore.
- Cannot start with a number.
- Can only contain alphanumeric characters and underscores (
_). - Case-sensitive (e.g.,
AGE,age,Ageare three different variable names). - Keywords cannot be used as variables (e.g.,
and,or,if,def).