[go: up one dir, main page]

0% found this document useful (0 votes)
30 views6 pages

Basics of Python

The document provides an overview of basic Python concepts, including variables, data types, functions, loops, and classes. It also includes common questions and answers related to Python programming, covering topics such as variable declaration, list creation, and exception handling. Overall, it serves as a foundational guide for beginners learning Python.
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)
30 views6 pages

Basics of Python

The document provides an overview of basic Python concepts, including variables, data types, functions, loops, and classes. It also includes common questions and answers related to Python programming, covering topics such as variable declaration, list creation, and exception handling. Overall, it serves as a foundational guide for beginners learning Python.
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/ 6

BASICS OF PYTHON

PYTHON BASICS TERMS -

1. Variable
A variable is a name given to a piece of data. It can store values like numbers, text, or more
complex data. For example, `x = 10` stores the number 10 in the variable `x`.

2. Data Type
Data types specify the kind of value a variable holds. Common data types in Python include
integers (e.g., 5), floats (e.g., 3.14), strings (e.g., "hello"), and booleans (e.g., True or False).

3. String
A string is a sequence of characters enclosed in quotes. For example, `"Hello, world!"` is a
string.

4. Integer
An integer is a whole number, positive or negative, without any decimal point. For example,
`7` and `-3` are integers.

5. Float
A float is a number with a decimal point. For example, `3.14` and `-0.5` are floats.

6. List
A list is a collection of items enclosed in square brackets and separated by commas. For
example, `[1, 2, 3]` is a list of integers.

7. Tuple
A tuple is similar to a list but is immutable, meaning its contents cannot be changed after
creation. It is enclosed in parentheses. For example, `(1, 2, 3)` is a tuple.

8. Dictionary
A dictionary is a collection of key-value pairs enclosed in curly braces. Each key is unique,
and it maps to a value. For example, `{'name': 'Alice', 'age': 25}` is a dictionary.
9. Function
A function is a block of reusable code that performs a specific task. It is defined using the
`def` keyword. For example, `def greet():` defines a function named `greet`.

10. Loop
A loop allows you to execute a block of code repeatedly. The two main types of loops in
Python are `for` loops and `while` loops.

11. If Statement
An `if` statement lets you execute code only if a certain condition is true. For example, `if x >
10:` checks if `x` is greater than 10.

12. Else Statement


An `else` statement provides an alternative block of code to execute if the `if` condition is
false. For example, `else:` executes if the `if` condition is not met.

13. Elif Statement


An `elif` statement stands for "else if" and lets you check multiple conditions. For example,
`elif x == 10:` checks if `x` is equal to 10.

14. Class
A class is a blueprint for creating objects with specific attributes and methods. It is defined
using the `class` keyword. For example, `class Dog:` defines a class named `Dog`.

15. Object
An object is an instance of a class. It contains attributes and methods defined in the class.
For example, `my_dog = Dog()` creates an object of the `Dog` class.
16. Method
A method is a function defined within a class that operates on objects of that class. For
example, `def bark(self):` is a method that could be defined in a `Dog` class.

17. Import
The `import` keyword is used to include external code modules into your script. For example,
`import math` allows you to use functions from the `math` module.

18. Exception
An exception is an error that occurs during program execution. You handle exceptions using
`try` and `except` blocks. For example, `try:` followed by `except:` lets you manage errors.

19. File
A file is a document used to store data. In Python, you can open, read, write, and close files
using functions like `open()`, `read()`, and `write()`.

20. Module
A module is a file containing Python code that can be imported and used in other scripts. For
example, `math` is a module that provides mathematical functions.

BASIC QUESTIONS WITH ANSWERS –


1. What is Python?
Python is a high-level, interpreted programming language known for its readability and
simplicity. It supports multiple programming paradigms.

2. What are the main features of Python?


Python features include readability, simplicity, extensive libraries, dynamic typing, and
support for various programming paradigms.

3. How do you declare a variable in Python?


You declare a variable by assigning a value to it. For example, `x = 10` declares a variable `x`
and assigns it the value `10`.

4. What are Python’s built-in data types?


Python’s built-in data types include integers, floats, strings, lists, tuples, dictionaries, and
booleans.

5. How do you create a list in Python?


You create a list by enclosing elements in square brackets. For example, `my_list = [1, 2, 3]`.

6. What is the difference between a list and a tuple?


A list is mutable, meaning you can change its contents, while a tuple is immutable and
cannot be modified after creation.

7. How do you access elements in a list?


You access elements by their index. For example, `my_list[0]` accesses the first element of
the list.

8. How do you add an element to a list?


You use the `append()` method to add an element. For example, `my_list.append(4)` adds `4`
to the end of the list.

9. What is a dictionary in Python?


A dictionary is a collection of key-value pairs. For example, `my_dict = {'name': 'Alice', 'age':
25}`.

10. How do you retrieve a value from a dictionary?


You retrieve a value by using its key. For example, `my_dict['name']` retrieves the value
`'Alice'`.

11. How do you define a function in Python?


You define a function using the `def` keyword. For example:
```python
def greet():
print("Hello!")
```

12. What is the purpose of the `return` statement in a function?


The `return` statement is used to return a value from a function to the caller.

13. How do you handle exceptions in Python?


You handle exceptions using `try` and `except` blocks. For example:
```python
try:
# code that might raise an exception
except Exception as e:
# code to handle the exception
```

14. What is a class in Python?


A class is a blueprint for creating objects with specific attributes and methods.

15. How do you create an object from a class?


You create an object by calling the class name. For example, `obj = MyClass()` creates an
object of the `MyClass` class.

16. What is inheritance in Python?


Inheritance is a feature that allows a class to inherit attributes and methods from another
class.

17. How do you read from a file in Python?


You use the `open()` function and the `read()` method. For example:
```python
with open('file.txt', 'r') as file:
content = file.read()
```

18. What is a lambda function?


A lambda function is an anonymous function defined with the `lambda` keyword. For
example:
```python
add = lambda x, y: x + y
```

19. How do you install packages in Python?


You use the `pip` tool. For example, `pip install package_name`.

20. What is list comprehension?


List comprehension is a concise way to create lists. For example:
```python
squares = [x**2 for x in range(10)]
```

You might also like