Python notes covering syntax, variables, data types, input/output, type conversion, arithmetic operators, and basic programs.
Definition: Python is a high-level, interpreted, general-purpose programming language known for its simple and readable syntax.
Example:
print("Hello, World!")Output:
Hello, World!
Definition: Syntax refers to the rules used to write valid Python code.
Example:
if 10 > 5:
print("10 is greater")Output:
10 is greater
Note: Python uses indentation (spaces at the beginning of a line) to define code blocks.
Definition: Comments are notes written in code that Python ignores during execution.
Syntax:
# This is a single-line commentExample:
# Display a message
print("Welcome to Python")Output:
Welcome to Python
Definition: A variable is a name that refers to a value stored in a program.
Syntax:
variable_name = valueExample:
name = "Priya"
age = 19
print(name)
print(age)Output:
Priya
19
Definition: Data types specify the kind of value a variable holds.
Common built-in data types:
| Data Type | Meaning | Example |
|---|---|---|
int |
Integer numbers | 19 |
float |
Decimal numbers | 5.0 |
str |
Text | "Python" |
bool |
Boolean values | True, False |
Example:
age = 19
height = 5.0
name = "Priya"
is_student = True
print(type(age))
print(type(height))
print(type(name))
print(type(is_student))Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
Definition: The print() function displays information on the screen.
Syntax:
print(value)Example:
print("Python")
print(10)
print(5 + 3)Output:
Python
10
8
Definition: The input() function accepts information entered by the user. By default, it returns a string (str).
Syntax:
variable = input("Enter something: ")Example:
name = input("Enter your name: ")
print("Hello", name)Sample Output:
Enter your name: Priya
Hello Priya
Definition: Type conversion means converting a value from one data type to another.
Common functions: int(), float(), str()
Example:
number = "25"
converted_number = int(number)
print(converted_number + 5)Output:
30
Definition: Arithmetic operators perform mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | 5 + 2 |
- |
Subtraction | 5 - 2 |
* |
Multiplication | 5 * 2 |
/ |
Division | 5 / 2 |
// |
Floor division | 5 // 2 |
% |
Modulus (remainder) | 5 % 2 |
** |
Exponentiation | 5 ** 2 |
Example:
a = 10
b = 3
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a ** b)Output:
13
7
30
3.3333333333333335
3
1
1000
Definition: A program can use variables and arithmetic operators to perform calculations.
Code:
a = 10
b = 20
total = a + b
print("Sum:", total)Output:
Sum: 30
Definition: Swapping means exchanging the values of two variables.
Code:
a = 5
b = 10
a, b = b, a
print("a =", a)
print("b =", b)Output:
a = 10
b = 5
Definition: The square of a number is the number multiplied by itself.
Code:
number = 6
square = number ** 2
print("Square:", square)Output:
Square: 36
Definition: Temperature conversion changes a temperature from one measurement scale to another.
Formula:
Fahrenheit = (Celsius × 9/5) + 32
Code:
celsius = 25
fahrenheit = (celsius * 9 / 5) + 32
print("Fahrenheit:", fahrenheit)Output:
Fahrenheit: 77.0
Definition: The area of a rectangle is calculated by multiplying its length by its width.
Formula:
Area = Length × Width
Code:
length = 10
width = 5
area = length * width
print("Area:", area)Output:
Area: 50
Try solving these questions yourself before checking other solutions.
- Print your name, age, and favourite programming language.
- Create two variables and calculate their sum, difference, and product.
- Take a user's name as input and display a greeting.
- Convert a string containing a number into an integer.
- Calculate the area of a circle.
- Swap two variables without using a third variable.
- Convert kilometres into metres.
- Calculate the average of three numbers.