What are some good Python examples for beginners?


In this article , we will learn some useful basic Python examples for beginners in this article.This article also includes some basic questions that are asked in the python interview. Lets get started!!!

How can I Make a Tuple out of a List?

Using the Python tuple() method, we may convert a list to a tuple. We can't update the list after it's been converted to a tuple since tuples are immutable.

Example

The following program returns the converts the list into a tuple using tuple() function −

# input list
inputList = ['hello', 'tutorialspoint', 'python', 'codes'] 

# converting input list into a tuple
resultTuple = tuple(inputList)  
# printing the resultant tuple 
print(resultTuple)  
# Printing the type of resultant tuple 
print(type(resultTuple))  

Output

('hello', 'tutorialspoint', 'python', 'codes')
<class 'tuple'>

What is a NumPy Array?

NumPy arrays are far more versatile than Python lists. NumPy arrays make reading and writing objects faster and more efficient.

In Python, in What Ways can you Make an Empty NumPy array and Numpy array of a Given Shape?

Example

The following program shows how to create an empty NumPy array and Numpy garbage array of a given shape −

# importing NumPy module 
import numpy  
 
# Creating an empty NumPy array without shape
array1 = numpy.array([])  
# printing array
print(array1)  
# Creating a NumPy array with given shape and garbage values
array2 = numpy.empty(shape=(3,3))  
print(array2) 

Output

[]
[[4.14578705e-316 1.77863633e-322 0.00000000e+000]
 [0.00000000e+000 2.37663529e-312 7.87101931e-071]
 [3.88586518e-033 5.03180591e-091 1.20858772e+161]]

What is a Negative Index in Python?

Python has a unique feature called negative indexing in arrays and lists.

Python allows for "indexing from the end," i.e., negative indexing.

This means that the last value in a sequence has an index of -1, the second last has an index of -2, and so on.

When you want to pick values from the end (right side) of an iterable, you can utilize negative indexing to your benefit.

What is the Python Data Type SET, and how can I use it?

"set" is a Python data type that is a sort of collection. It has been a part of Python since version 2.4. A set is a collection of distinct and immutable items that are not ordered in any specific way.

How do you Print the Summation of all the Numbers From 1 to 100?

Example

The following program returns the sum of the first 100 natural numbers −

# printing the sum of numbers from 1 to 100
print(sum(range(1,101)) )  

Output

5050

What is the Difference Between a List and a Tuple in Python?

List Tuple
Lists are editable, which means they may be mutable. Tuples are immutable, which means we cannot change the elements of a tuple.
Lists are relatively slower. Tuples beat lists in terms of efficiency.
Syntax
list = [40, tutorialspoint, 100]
Syntax
tuple = (40, tutorialspoint, 100)

Is Python a Programming Language or Scripting Language?

Although we can write scripts using Python, it is primarily utilized as a general-purpose programming language.

Python is an Interpreted Programming Language. Explain.

An interpreted language is any scripting language that is not in machine code before execution. As a result, Python is an interpreted language. Additionally, because it is an interpreted language, it cannot be converted to computer-readable code before running at runtime.

What is pep 8?

PEP is an abbreviation for Python Enhancement Proposal. It is a collection of guidelines for formatting Python code for better readability.

What are Decorators in Python?

Decorators are only used to add certain layout patterns to a method without impacting the function's structure. Decorators are typically identified prior to the event for which they will be improving. Before we can use a decorator, we must first define its function i,e decorator function.

The function in which the decorator's function will be implemented is then written, and the decorator function is simply positioned above it. The @ symbol comes before the decorator in this case.

What are the Most Prevalent Python built-in Data Types?

Numbers − Python's most common built-in data structures are integers, complex numbers, and floating points.

Example 

5, 2+3i, 3.5.

List − A list is a collection of objects ordered in a particular order. The components of a list can be of multiple data types.

Example 

[10, ‘tutorialspoint’, 4.89]

Tuple − A tuple is a set of items in a specific order. Tuples, unlike lists, are immutable, which means they cannot be changed.

Example 

(10, ‘tutorialspoint’, 4.89)

String − A string is a collection of characters. Strings can be declared using the single or double quotes.

Example 

“Hello ‘tutorialspoint’”.

Set − A set is a collection of unrelated items that are not in any specific order.

Example 

(5, 2, 8, 1)

Dictionary − A dictionary is a collection of key and value pairs in which each value can be accessed by its key. The order/sequence of the items is irrelevant.

Example 

{10:’tutorialspoint’, 20:python}

What is the self in Python?

A self is a class instance or an object. In Python, this is explicitly specified as the first parameter. That is not the situation in Java, where it is optional. Local variables help distinguish between a class's methods and attributes.

The self variable of a class corresponds to newly created objects in the init method, but it refers to the entity whose method can be invoked in other methods of the class.

How do These Commands work: Break, pass and Continue?

break − When a condition is satisfied, the loop is terminated, and control is transferred to the next statement.

pass − When a code block needs to be syntactically valid but you don't want to run it, use this pass statement. In essence, this is a null action. When it is executed, nothing happens.

continue − Control is sent to the beginning of the loop when a specified condition is met, allowing some portion of the loop's current execution to be skipped.

How will you Convert Every Character in the String to Lowercase Letters?

To convert a string to lowercase, use the lower() function.

Example

The following program converts every character in the string to lowercase letters −

# input string
inputString = 'TUTORIALSPOINT'  

# converting each character of the string into lowercase
# using the lower() function
print("Converting every character of the string into lowercase:")
print(inputString.lower())

Output

Converting every character of the string into lowercase:
tutorialspoint

Conclusion

In this article, we learned 16 different Python examples. These are also crucial questions for Python interviews.

Updated on: 01-Feb-2023

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements