[go: up one dir, main page]

0% found this document useful (0 votes)
17 views4 pages

Grade 8 Chapter-3

The document provides an overview of basic programming concepts in Python, focusing on variables and lists. It explains how to create and manipulate lists, including appending elements, using loops, and performing operations like editing and deleting elements. Additionally, it covers validation, menu-driven programs, and sorting methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views4 pages

Grade 8 Chapter-3

The document provides an overview of basic programming concepts in Python, focusing on variables and lists. It explains how to create and manipulate lists, including appending elements, using loops, and performing operations like editing and deleting elements. Additionally, it covers validation, menu-driven programs, and sorting methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Computational thinking

Variable:
A variable is a naming memory location, it can store one value or a piece of data. we can assign
values to a variable with the help of ‘=’ operator.
Example: a=10
Name=”Saradhi’
Naming rules:
1. Variable name must starts with a letter or underscore symbol(_)
2. Variable name should not be a keyword
3. Variable name should not contain any spaces
4. It can be a combination of letters, numbers and symbol(_)
5. Python is a case sensitive language (a!=A)
List:
List is a special data type, we can store multiple values in a list that are belongs to same data
type or different datatype. the list values can be enclosed in square brackets.
Example: list1=[10,20,3,40,50]
List2=[‘a’,’b’,’c’,’f’]
List3=[’10,’a’,’b’,30]
Working with List:
Index Value: These are the values that can be used as address of the value. These values tells the
position of the element in the list. Index values by default starts with 0.
list1=[10,20,30,40,50]
Index values 0 1 2 3 4

Appending an element:
Append is the method used to add a new value at the ending location.
Example: list1=[10,20,3,40,50]
List1.append(530)
After Adding, the final list values are
list1=[10,20,3,40,50,530]
Appending number of elements using loops:
Loop is the concept used to repeat same statements multiple number of times.by using different
looping statements we can add multiple values into a list at the same time.
The following are the different types of loops in python
1. For loop (counter loop)
2. While loop (condition loop)
For loop is also called as counter loop, it uses a temporary variable for counting and also
another function called range which can be used to tell the upper value(ending). This loop is use
full when we know the exact count of repetitions.
Example: for I in range(1,100):
Print(i)
In the above given example I is the default variable which starts from 1 and range is a function
which tells us about the ending value(99).
Example Program (rainbow colors):
Colors=[]
For I in range(1,8):
Col=input(‘enter rainbow color’)
Colors.append(col)
Print(colors)
While loop:
A while loop can also be called as conditional loop. the loop will be controlled by a condition.
this loop will run as long as your condition is true. If the condition is false, the loop will be
terminated.
Example:
Colors=[]
Repeat=’Y’
While Repeat==’Y:
Col=input(‘enter rainbow color’)
Colors.append(col)
Repeat=input(‘do you want to add another(Y/N)’)
Print(colors)
Printing list elements:
Single/multiple values of the list can be printed with the help of index values.
Example: Values=[100,200,30,400]
Print(Values[2])
Editing a list element:
Edit is the option used to replace the old value with a new value. To update any list, we use the
index value.
Example: values=[100,200,30,400]
Values[2]=300
2nd index value has been replaced with 300(old value is 30)
Deleting an element:
Del is the command used to delete an element from the list.
Values=[100,200,30,400]
Del Values[3]
400 will be deleted from the list.
Length function:
len() is the function used to find the length of the given list. Length means number of elements
in the list.The same function can also used to find the length of the string.
Example: len(‘hello’)
Output:5
Example: Values=[100,200,30,400]
Print(len(Values))
Output: 4
Validation:
Validation is used to validate the condition. If the condition is true, we are going to execute
some set of statements, if the condition is false, we are going to execute another set of
statements.
Example: if a>b and a>c:
Print(‘a is the bigger value’)
Elif b>c and b>a:
Print(‘b is the bigger value’)
Else:
Print(‘c is the bigger value’)
Menu of choices:
Team Manager:
This program will help you to manage your team:
Choose the option:
A: To append a value
P: To print the team list
X: To exit from the program
In the above given example, we have 3 options to select that performs different operations, as
per the requirement we are going to write the program.

Program:
Choice=' '
Mylist=[]
while Choice!='X':
print('T E A M M A N A G E R')
print('=============================')
print('this program will help you to manage your team\n')
print(' A: To append a value\n')
print('P: To print the team list\n')
print('X: To exit from the program\n')
Choice=input('enter your choice')
if Choice=='A':
i=int(input('enter the value to append to the list'))
Mylist.append(i)
print('the entered value has been appended in the list successfully')
elif Choice=='P':
print(Mylist)
elif Choice=='X':
exit()
else:
print('the entered value is invalid')
Sorting:
The process of arranging values in an order is called as sorting, there are 2 types of sortings.
Ascending order: sorting the elements in increasing order
Descending order: sorting the elements in decreasing order

You might also like