Skip to content

Latest commit

 

History

History
363 lines (245 loc) · 5.11 KB

01_python_basics.md

File metadata and controls

363 lines (245 loc) · 5.11 KB

Python Basics

Python notes covering syntax, variables, data types, input/output, type conversion, arithmetic operators, and basic programs.


1. What is Python?

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!

2. Python Syntax

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.

3. Comments

Definition: Comments are notes written in code that Python ignores during execution.

Syntax:

# This is a single-line comment

Example:

# Display a message
print("Welcome to Python")

Output:

Welcome to Python

4. Variables

Definition: A variable is a name that refers to a value stored in a program.

Syntax:

variable_name = value

Example:

name = "Priya"
age = 19

print(name)
print(age)

Output:

Priya
19

5. Data Types

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'>

6. The print() Function

Definition: The print() function displays information on the screen.

Syntax:

print(value)

Example:

print("Python")
print(10)
print(5 + 3)

Output:

Python
10
8

7. The input() Function

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

8. Type Conversion

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

9. Basic Arithmetic Operators

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

10. Basic Program: Add Two Numbers

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

11. Basic Program: Swap Two Variables

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

12. Basic Program: Calculate the Square

Definition: The square of a number is the number multiplied by itself.

Code:

number = 6
square = number ** 2

print("Square:", square)

Output:

Square: 36

13. Basic Program: Convert Celsius to Fahrenheit

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

14. Basic Program: Calculate Area of a Rectangle

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

Practice Questions

Try solving these questions yourself before checking other solutions.

  1. Print your name, age, and favourite programming language.
  2. Create two variables and calculate their sum, difference, and product.
  3. Take a user's name as input and display a greeting.
  4. Convert a string containing a number into an integer.
  5. Calculate the area of a circle.
  6. Swap two variables without using a third variable.
  7. Convert kilometres into metres.
  8. Calculate the average of three numbers.