Chapter 2-Programming Basics and Arrays.pptx Chapter 2-Programming Basics and Arrays.pptx
YosefNigussie2
10 views
23 slides
Mar 10, 2025
Slide 1 of 23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
About This Presentation
Chapter 2-Programming Basics and Arrays.pptx
Size: 819.1 KB
Language: en
Added: Mar 10, 2025
Slides: 23 pages
Slide Content
By: Yosef Nigussie 1 Python Basics
2 Python Basics
Basic Syntax . 3
Python Variables Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.. Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. python allows you to assign a single value to several variables or multiple objects to multiple variables simultaneously. 4 a = b = c = 1 a,b,c = 1,2,"john"
Data types Python has five standard data types Numbers String List Tuple Dictionary 5
Python Numbers Python supports four different numerical types: int (signed integers) long (long integers, they can also be represented in octal and hexadecimal) float (floating point real values) complex (complex numbers) 6 var1 = 1 var2 = 10 del var del var_a , var_b
Python Strings Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ([ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end. The plus (+) sign is the string concatenation operator and the asterisk (*) is the repetition operator. 7
Example User inputs: Enter your name: John Enter your age:25 Out put: Welcome, John ! you are now 25 Write a program which accepts name and age input from user and print it? 8
Python Operators Assignment operators(=) Arithmetic operators(+,*,/,-) Comparison operators(<,>,<=) Logical operators(and, or, not) Bitwise operators(^,&,|) Data Type Conversions: Implicit or explicit 9
Arrays An array is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key Arrays and Lists Index Notation Displaying Array Members Multidimensional Arrays 10
Python Lists The most basic data structure in Python is the sequence . Each element of a sequence is assigned a number - its position or index. The first index is zero, the second index is one, and so forth. The list is a most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that items in a list need not be of the same type. Lists are mutable objects that can change their values 11 list1 = ['physics', 'chemistry', 1997, 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 = ["a", "b", "c", "d"]
Python Lists Accessing Values in Lists: use the square brackets for slicing along with the index or indices to obtain value available at that index. Updating Lists: You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. 12
Python Lists Delete List Elements: To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting or the remove() method if you do not know. Basic List Operations: Lists respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new list, not a string. 13
Python Lists Common List functions 14
Python Lists Common list methods 15
Tuples A tuple is a collection of objects which ordered and immutable . The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. 16 tup1 = ('physics', 'chemistry', 1997, 2000); tup2 = (1, 2, 3, 4, 5 ); tup3 = "a", "b", "c", "d";
Tuples Accessing Values in Tuples Updating Tuples Delete Tuple Elements Basic Tuples Operations: Tuples respond to the + and * operators much like strings; they mean concatenation and repetition here too, except that the result is a new tuple, not a string. 17
Tuples Built-in Tuple Functions 18
Dictionaries Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces . Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples. 19
Dictionaries Accessing Values in Dictionary Updating Dictionary Delete Dictionary Elements 20
Dictionaries Properties of Dictionary Keys More than one entry per key not allowed. Which means no duplicate key is allowed. Keys must be immutable. 21 dict = { 'Name' : 'Zara' , 'Age' : 7 , 'Name' : ' Manni ' } print " dict ['Name']: " , dict [ 'Name' ] dict = {[ 'Name' ]: 'Zara' , 'Age' : 7 } print " dict ['Name']: " , dict [ 'Name' ]