0% found this document useful (0 votes)
10 views4 pages

Python Basics Explained

Uploaded by

azeemrafiq44
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)
10 views4 pages

Python Basics Explained

Uploaded by

azeemrafiq44
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/ 4

Python Basics with Full Explanation

1. Variables and Data Types

Python uses dynamic typing, so you don't need to specify types explicitly.

Examples:

name = "Azeem" # string

age = 25 # integer

height = 5.9 # float

is_student = True # boolean

Comparison with JavaScript:

JavaScript: let x = 5;

Python: x = 5

2. Printing Output

Use the print() function to display output.

Example:

print("Hello, world!")

print("Name:", name)

3. User Input

Use input() to take input from users. It returns a string, so you may need to convert it.

Example:

name = input("Enter your name: ")

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


Python Basics with Full Explanation

4. Conditions

Use if, elif, and else to make decisions.

Example:

if age >= 18:

print("Adult")

elif age >= 13:

print("Teenager")

else:

print("Child")

5. Loops

for loop:

for i in range(5): # 0 to 4

print(i)

while loop:

i=0

while i < 5:

print(i)

i += 1

6. Lists

Python lists are similar to arrays in JavaScript.

Example:
Python Basics with Full Explanation

fruits = ["apple", "banana", "cherry"]

print(fruits[0])

fruits.append("mango")

print(len(fruits))

7. Functions

Functions are defined using def keyword.

Example:

def greet(name):

print("Hello", name)

greet("Azeem")

8. Dictionaries

Dictionaries are like JS objects with key-value pairs.

Example:

person = {"name": "Azeem", "age": 25}

print(person["name"])

9. String Formatting

Use f-strings to include variables inside strings.

Example:

name = "Azeem"
Python Basics with Full Explanation

age = 25

print(f"My name is {name} and I am {age} years old.")

10. Comments

Single-line comment:

# This is a comment

Multi-line comment:

"""

This is a

multi-line comment

"""

You might also like