0% found this document useful (0 votes)
6 views7 pages

Python Basics Detailed Explained

Uploaded by

nakshvardhan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Python Basics Detailed Explained

Uploaded by

nakshvardhan05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Basics for Beginners (Detailed with Examples)

1. Introduction to Python

Python is a simple and powerful programming language used in web development, data science, AI,

and more.

It has an easy-to-read syntax, making it a great choice for beginners.

Example:

# This is a comment in Python

print("Python is fun!")

Output:

Python is fun!

2. Writing Your First Python Program

To display something on the screen, we use the print() function.

Example:

print("Hello, World!")

Output:

Hello, World!

Explanation:

- The print() function is used to display text or values on the screen.

- Anything inside quotes (" ") is treated as text.


3. Variables and Data Types

Variables are used to store data. Python automatically detects the data type.

Example:

x = 10 # Integer

y = 3.5 # Float

name = "Alice" # String

is_student = True # Boolean

Explanation:

- Integer (int): Whole numbers, e.g., 10, -5, 1000.

- Float: Decimal numbers, e.g., 3.5, 2.0, -7.8.

- String: Text values enclosed in quotes, e.g., "Alice", "Python".

- Boolean: Represents True or False values.

4. Taking User Input

We can take input from the user using the input() function.

Example:

name = input("Enter your name: ")

print("Hello, " + name)

Explanation:

- input() takes user input as a string.

- The `+` operator joins (concatenates) the string with another string.
5. Operators in Python

Operators are used to perform calculations and comparisons.

Example:

a = 10

b=5

print("Addition:", a + b) # Output: 15

print("Subtraction:", a - b) # Output: 5

print("Multiplication:", a * b) # Output: 50

print("Division:", a / b) # Output: 2.0

print("Modulus:", a % b) # Output: 0

Explanation:

- `+` adds numbers.

- `-` subtracts numbers.

- `*` multiplies numbers.

- `/` divides numbers.

- `%` finds the remainder after division.

6. Conditional Statements (if-else)

Conditional statements help in decision-making.

Example:

age = int(input("Enter your age: "))

if age >= 18:

print("You can vote.")

else:
print("You cannot vote.")

Explanation:

- The `if` condition checks if the user is 18 or older.

- If the condition is true, it prints "You can vote."

- Otherwise, the `else` statement runs.

7. Loops in Python

Loops allow repeating a block of code multiple times.

For Loop Example:

for i in range(1, 6):

print(i)

Output:

Explanation:

- `range(1, 6)` generates numbers from 1 to 5.

- The loop prints each number one by one.

While Loop Example:

x=1
while x <= 5:

print(x)

x += 1

Output:

Explanation:

- The loop continues as long as `x` is less than or equal to 5.

- `x += 1` increases `x` by 1 in each step.

8. Functions in Python

Functions help organize and reuse code.

Example:

def greet(name):

print("Hello, " + name + "!")

greet("Alice")

Output:

Hello, Alice!
Explanation:

- The `def` keyword defines a function.

- The function `greet()` takes a parameter `name` and prints a message.

9. Lists in Python

Lists store multiple values.

Example:

fruits = ["Apple", "Banana", "Cherry"]

print(fruits[1]) # Output: Banana

Explanation:

- Lists are written in square brackets `[ ]`.

- Indexing starts from 0, so `fruits[1]` gives "Banana".

10. Dictionaries in Python

Dictionaries store key-value pairs.

Example:

student = {"name": "John", "age": 16, "grade": "A"}

print(student["name"]) # Output: John

Explanation:

- Dictionaries use `{}` and store data as key-value pairs.

- `"name"` is a key, and `"John"` is its value.

This guide includes detailed explanations and examples to help you learn Python better. Keep
practicing and happy coding!

You might also like