Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are some good Python examples for beginners?
This article provides essential Python examples for beginners, covering data structures, basic operations, and commonly asked interview questions. These examples demonstrate fundamental concepts that form the building blocks of Python programming.
Converting a List to a Tuple
Using the Python tuple() function, you can convert a list to a tuple. After conversion, the data becomes immutable since tuples cannot be modified ?
# 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))
('hello', 'tutorialspoint', 'python', 'codes')
<class 'tuple'>
Creating NumPy Arrays
NumPy arrays are more efficient than Python lists for numerical computations. You can create empty arrays or arrays with specific shapes ?
# importing NumPy module
import numpy
# creating an empty NumPy array
array1 = numpy.array([])
print("Empty array:", array1)
# creating a NumPy array with given shape and uninitialized values
array2 = numpy.empty(shape=(2,3))
print("Array with shape (2,3):")
print(array2)
Empty array: [] Array with shape (2,3): [[4.67296746e-307 1.69121096e-306 1.33511018e-306] [1.78020169e-306 7.56571288e-307 3.11525958e-307]]
Understanding Negative Indexing
Python supports negative indexing, allowing you to access elements from the end of a sequence. The last element has index -1, second last has -2, and so on ?
words = ['apple', 'banana', 'cherry', 'date']
print("Last element:", words[-1])
print("Second last:", words[-2])
print("First element:", words[-4])
Last element: date Second last: cherry First element: apple
Working with Sets
A set is a collection of unique and unordered elements. It's useful for removing duplicates and performing set operations ?
# creating a set
fruits = {'apple', 'banana', 'cherry', 'apple'}
print("Set (duplicates removed):", fruits)
# set operations
numbers1 = {1, 2, 3, 4}
numbers2 = {3, 4, 5, 6}
print("Union:", numbers1 | numbers2)
print("Intersection:", numbers1 & numbers2)
Set (duplicates removed): {'cherry', 'banana', 'apple'}
Union: {1, 2, 3, 4, 5, 6}
Intersection: {3, 4}
Calculating Sum of Numbers
You can quickly calculate the sum of numbers using built-in functions ?
# sum of numbers from 1 to 100
print("Sum of 1 to 100:", sum(range(1, 101)))
# sum of a list
numbers = [10, 20, 30, 40, 50]
print("Sum of list:", sum(numbers))
Sum of 1 to 100: 5050 Sum of list: 150
List vs Tuple Comparison
| List | Tuple |
|---|---|
| Mutable (can be changed) | Immutable (cannot be changed) |
| Slower performance | Faster performance |
Syntax: [1, 2, 3]
|
Syntax: (1, 2, 3)
|
| Uses more memory | Uses less memory |
Python Built-in Data Types
Numbers: Integers, floats, and complex numbers for mathematical operations.
integer_num = 42
float_num = 3.14
complex_num = 2 + 3j
print(f"Integer: {integer_num}, type: {type(integer_num)}")
print(f"Float: {float_num}, type: {type(float_num)}")
print(f"Complex: {complex_num}, type: {type(complex_num)}")
Integer: 42, type: <class 'int'> Float: 3.14, type: <class 'float'> Complex: (2+3j), type: <class 'complex'>
Control Flow Statements
break: Exits the loop when a condition is met.
continue: Skips the current iteration and moves to the next.
pass: Does nothing, used as a placeholder ?
for i in range(5):
if i == 2:
continue # skip when i is 2
if i == 4:
break # exit loop when i is 4
print(f"Number: {i}")
# pass example
def future_function():
pass # placeholder for future implementation
print("Loop completed")
Number: 0 Number: 1 Number: 3 Loop completed
String Case Conversion
Use the lower() method to convert strings to lowercase ?
# input string
inputString = 'TUTORIALSPOINT'
# converting to lowercase
print("Original:", inputString)
print("Lowercase:", inputString.lower())
print("Uppercase:", inputString.upper())
print("Title case:", inputString.title())
Original: TUTORIALSPOINT Lowercase: tutorialspoint Uppercase: TUTORIALSPOINT Title case: Tutorialspoint
Conclusion
These Python examples cover essential concepts including data type conversions, built-in data structures, and basic operations. Mastering these fundamentals will provide a solid foundation for more advanced Python programming.
