Thursday, 23 February 2023

3 Ways to store numbers in a python list

There are many methods to store numbers in a python list. Lets explore top 3 ways to store numbers in a python list. 

Top 3 ways to store numbers in a python list. To understand this, we will write a python code to store contact number of 3 students.

Python code to store contact number of 3 students in a list.

Example 1: 

# first create an empty list to store contact numbers
mobile_numbers = []

# add mobile numbers of 3 students to the list
mobile_numbers.append("2345678901")
mobile_numbers.append("3456789012")
mobile_numbers.append("4567890123")

# print the list to verify
print(mobile_numbers)


Description of the example 1:

This code will create an empty list (name of list: mobile_numbers) and then adds 3 mobile numbers to it. You can replace the mobile numbers with the actual mobile numbers of the students. Using append() function, add elements to the list. The print() function is used to verify the list.


Example 2

mobile_numbers = ['2345678901', '3456789012', '4567890123']
print(mobile_numbers)


Example 3

# Create an empty list to store contact numbers
student_numbers = []

# Read contact numbers and append it to a list

for i in range(5):
    number = input(f"Enter contact number of student{i+1}: ")
    student_numbers.append(number)

# Print list of contact numbers
print("Contact numbers of 5 students:")
print(student_numbers)

*  *  *  *  *


Thursday, 16 February 2023

Brief note on Python Collections

Python Collections

Python collections are containers used to store and manage a collection of related data. These containers are built-in data types in Python and provide several operations to efficiently manipulate data. The Python collections include:

  1. List - It is an ordered collection of elements that can be of any data type. Lists are mutable, i.e., you can add, remove or modify elements.
  2. Tuple - It is an ordered collection of elements that can be of any data type. However, tuples are immutable, i.e., you cannot change the elements.
  3. Set - It is an unordered collection of unique elements. Sets are mutable, and you can add or remove elements.
  4. Dictionary - It is an unordered collection of key-value pairs. Dictionaries are mutable, and you can add, remove, or modify key-value pairs.

Python collections provide several functions and methods that allow you to perform various operations on the data. Some of these operations include sorting, filtering, searching, and iteration. 

By using Python collections, you can efficiently handle large amounts of data and build complex applications.

Get more details about Python programming collection at following link:


Top of Form

 

Saturday, 31 December 2022

Python program to search a word in a given string

  Python String  


Search a Word in a given String

  • We can use "in" keyword to search a word in a given string.

Example:


myText = "Wishing You All Happy and Healthy 2023 "
searchWord = "2023"

if (searchWord in myText):
    print(searchWord, "is present in myText variable")
else:
    print(searchWord, "is not present in myText variable")

Program Output :

      2023 is present in myText variable


Recommended Articles:

 


 Home 

 Next 

Monday, 11 July 2022

Python program to reverse a given string

  Python String  

Python program to reverse a given string. 

This program will read a string from user and prints reversed string.

Program Code :

# Python program to reverse a given string.

str=input("Enter your string:")
print("The original string is : " + str)

str_rev = str[::-1]
print("The reversed string is : " + str_rev)

Program Output :

Enter your string:Python Practicals 

The original string is : Python Practicals 

The reversed string is : slacitcarP nohtyP

Recommended Articles:

 


 Home 

 Next 

 

Tuesday, 17 August 2021

Program to count total number of characters in a given String

Following Python program will count total number of characters in a given string. 

Expected Output: If user enters string = Hello Students, output should be

Total Number of Characters in a String = 14

Program Code:

# Python program to count total number of characters in a given String
 
str1 = input("Enter your string: ")
count = 0  #initialize counter to 0
 
for i in str1:
    count = count + 1
 
print("Total Number of Characters in a String = ", count)

Output of Program:

Enter your string: Hello Students Total Number of Characters in a String = 14


* * * * *

< Back to Python Loops >


Saturday, 14 August 2021

Square pattern of star (*) using for loop

Following Python program will print square pattern of star (*) using for loop. Number of lines to be printed are entered by the user. 

Expected Output: If user enters lines = 3, output should be

* * * * * * * * *

Program Code:

lines = int(input("Enter number of lines:"))

for i in range(lines):    
   for i in range(lines):        
      print('*', end = '  ')    
   print()

Output of Program:

Enter number of lines:3 * * * * * * * * *


* * * * *


< Back to Python Loops >





Monday, 9 August 2021

Even numbers between 1 to 10 using for loop

Following Python program will print even numbers between 1 to 10 using for loop. 

Expected Output: 2 4 6 8 10 


Program Code:

for i in range(2, 11, 2):
  print (i, end=" ")

Output of Program:

2 4 6 8 10


* * * * *


< Back to Python Loops >





Sunday, 8 August 2021

Odd numbers between 1 to 10 using while loop

Following Python program will print odd numbers between 1 to 10 using while loop. 

Expected Output: 1 3 5 7 9 


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 2

Output of Program:

1 3 5 7 9


* * * * *


< Back to Python Loops >

< Back to Home >



Friday, 1 January 2021

Python program to print 10 to 1 using while

 Following Python program will print 10 to 1 numbers using while loop. 

Expected Output: 10 9 8 7 6 5 4 3 2 1


Program Code:

number = 10

while number >= 1:
  print(number, end=" ")
  number = number - 1


Output of Program:

10 9 8 7 6 5 4 3 2 1


* * * * *


< Back to Python Loops >

< Back to Home >

Python program to print 1 to 10 using while loop

Following Python program will print 1 to 10 numbers using while loop. 

Expected Output: 1 2 3 4 5 6 7 8 9 10


Program Code:

number = 1

while number <= 10:
  print(number, end=" ")
  number = number + 1


Output of Program:

1 2 3 4 5 6 7 8 9 10


* * * * *


< Back to Python Loops >

< Back to Home >