1.
Hello World Program
Printing "Hello, World!" is the simplest way to start learning Python. The `print()` function is used to
display text on the screen.
Example 1: Basic Hello World
```python
print("Hello, World!") #Hello, World!
```
Here, the `print()` function outputs the text `"Hello, World!"` to the console.
Example 2: Using a Variable
```python
message = "Hello, World!"#Hello, World!
print(message)
```
The string `"Hello, World!"` is stored in the variable `message`, and `print(message)` displays it.
Example 3: Using String Concatenation
```python
greeting = "Hello"
name = "World"
print(greeting + ", " + name + "!")#Hello, World!
```
The `+` operator concatenates (joins) strings to form the final message.
Example 4: Using an f-string (Python 3.6+)
```python
name = "World"
print(f"Hello, {name}!")#Hello, World!
```
The `f` before the string allows inserting variables inside curly braces `{}`.
2. Assigning Values
Assigning values to variables means storing data in a variable using the `=` operator.
Example 1: Assigning an Integer
```python
a = 10
print(a) # 10
print(type(a)) # <class 'int'>
```
Here, the integer value `10` is assigned to the variable `a`.
Example 2: Assigning a String
```python
name = "Alice"
print(name) # Alice
print(type(name)) # <class 'str'>
```
The string `"Alice"` is assigned to the variable `name`.
Example 3: Assigning a Float
```python
pi = 3.14
print(pi) # 3.14
print(type(pi)) # <class 'float'>
```
The floating-point number `3.14` is stored in `pi`.
Example 4: Assigning Multiple Values in One Line
```python
x, y, z = 1, 2.5, "hello"
print(x, y, z) # 1 2.5 hello
print(type(x), type(y), type(z)) # <class 'int'> <class 'float'> <class 'str'>
```
Here, `x`, `y`, and `z` are assigned different values of different types in a single line.
Example 5: Assigning the Same Value to Multiple
Variables
```python
a = b = c = 42
print(a, b, c) # 42 42 42
```
All three variables `a`, `b`, and `c` are assigned the value `42`.
3. Operators in Python
Operators perform operations on variables and values.
Example 1: Arithmetic Operators
```python
a = 10
b=3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Modulus:", a % b)
print("Exponentiation:", a**b)
print("Floor Division:", a // b)
```
Explanation : This program demonstrates arithmetic operators like `+`, `-`, `*`, `/`, `%`, ` `, and `//`.
Example 2: Comparison Operators
```python
x=5
y = 10
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= y) # False
print(x <= y) # True
```
Explanation : Comparison operators compare two values and return `True` or `False`.
Example 3: Assignment Operators
```python
x = 10
x += 5 # x = x + 5
print(x) # 15
x *= 2 # x = x * 2
print(x) # 30
x //= 3 # x = x // 3
print(x) # 10
```
Explanation : Assignment operators update variable values in a shorthand way.
4. Data Types (String, int, etc.)
Python has various data types like integers, floats, strings, and booleans.
Example 1: Integer and Float
```python
x = 10 # Integer
y = 3.14 # Float
print(type(x)) # <class 'int'>
print(type(y)) # <class 'float'>
```
Explanation : Integers are whole numbers, and floats are decimal numbers.
Example 2: String Operations
```python
name = "Python"
print(name[0]) # P
print(name[-1]) # n
print(name[1:4]) # yth
print([Link]()) # PYTHON
print([Link]()) # python
```
Explanation : Strings support indexing, slicing, and built-in methods.
Example 3: Boolean Type
```python
a = True
b = False
print(a and b) # False
print(a or b) # True
```
Explanation : Boolean values are either `True` or `False`.
5. Variable Conversions
Converting data from one type to another.
Example 1: Integer to String
```python
x = 100
y = str(x)
print(y) # "100"
print(type(y)) # <class 'str'>
```
Example 2: String to Integer
```python
s = "50"
num = int(s)
print(num + 10) # 60
```
Example 3: Float to Integer
```python
f = 5.99
i = int(f)
print(i) # 5
```
6. If, Else, and Elif Statements
Conditional statements to execute different code blocks.
Example 1: Simple If Statement
```python
x = 10
if x > 5:
print("x is greater than 5")
```
Explanation : Executes only if the condition is `True`.
Example 2: If-Else
```python
age = 18
if age >= 18:
print("You can vote")
else:
print("You cannot vote")
```
Explanation : If the condition is false, the `else` block runs.
Example 3: If-Elif-Else
```python
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
```
Explanation : Multiple conditions are checked in sequence.
Example 4: Nested If
```python
x = 10
if x > 5:
if x < 20:
print("x is between 5 and 20")
```
Explanation : A condition inside another condition.
7. Using `input()` for User Input
Getting user input from the console.
Example 1: Simple Input
```python
name = input("Enter your name: ")
print("Hello, " + name)
```
Example 2: Taking Integer Input
```python
num = int(input("Enter a number: "))
print("You entered:", num)
```
Example 3: Adding Two Numbers
```python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print("Sum:", a + b)
```
Example 4: Checking Even or Odd
```python
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
```