0% found this document useful (0 votes)
13 views

Basics of Python

Basics of python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Basics of Python

Basics of python
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python is a popular, beginner-friendly programming language known for its readability and

simplicity. Here’s an overview of some Python basics to get you started:

1. Hello, World!

 The classic first program. In Python, you can print text to the console with the
print() function:

python
Copy code
print("Hello, World!")

2. Variables and Data Types

 In Python, you don’t need to declare the data type of a variable. You can assign values
directly.

python
Copy code
name = "Alice" # String
age = 25 # Integer
height = 5.8 # Float
is_student = True # Boolean

3. Basic Data Types

 Integers (int): Whole numbers, e.g., 10


 Floats (float): Decimal numbers, e.g., 10.5
 Strings (str): Text, e.g., "Hello"
 Booleans (bool): True/False values, e.g., True or False

4. Operators

 Arithmetic: +, -, *, /, // (integer division), % (modulus), ** (power)


 Comparison: ==, !=, >, <, >=, <=
 Logical: and, or, not

5. Lists and Dictionaries

 List: An ordered, mutable collection.

python
Copy code
fruits = ["apple", "banana", "cherry"]
fruits.append("orange") # Adds "orange" to the list

 Dictionary: A collection of key-value pairs.


python
Copy code
student = {"name": "Alice", "age": 25, "is_student": True}
print(student["name"]) # Output: Alice

6. Conditionals

 Conditional statements allow you to execute code based on conditions.

python
Copy code
if age >= 18:
print("You are an adult.")
elif age > 13:
print("You are a teenager.")
else:
print("You are a child.")

7. Loops

 For Loop: Used to iterate over sequences (like lists or strings).

python
Copy code
for fruit in fruits:
print(fruit)

 While Loop: Repeats as long as a condition is true.

python
Copy code
count = 0
while count < 5:
print(count)
count += 1

8. Functions

 Functions allow you to create reusable blocks of code.

python
Copy code
def greet(name):
return f"Hello, {name}!"

print(greet("Alice")) # Output: Hello, Alice!

9. Classes and Objects

 Python is an object-oriented language, so you can create your own data types
(classes).

python
Copy code
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def introduce(self):
return f"My name is {self.name} and I am {self.age} years
old."

alice = Person("Alice", 25)


print(alice.introduce()) # Output: My name is Alice and I am 25
years old.

10. Importing Modules

 Python has many built-in modules that you can import, such as math for mathematical
functions.

python
Copy code
import math
print(math.sqrt(16)) # Output: 4.0

These are the core fundamentals of Python, and mastering these will give you a solid
foundation. From here, you can start working on small projects to practice your skills. Let me
know if you want more details on any specific topic!

You might also like