Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
13 views23 pages

Chapter 2-Programming Basics and Arrays

Uploaded by

GETACHEW AZMIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views23 pages

Chapter 2-Programming Basics and Arrays

Uploaded by

GETACHEW AZMIR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Python Basics

By: Micheale Hadera

1
Python Basics

2
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 a = b = c = 1
several variables or multiple objects to multiple
variables simultaneously. a,b,c = 1,2,"john"

4
Data types

 Python has five standard data types


 Numbers
 String
 List
 Tuple
 Dictionary

5
Python Numbers

 Python supports four different numerical types: var1 = 1 var2 = 10


 int (signed integers)
 long (long integers, they can also be
represented in octal and hexadecimal)
del var del var_a, var_b
 float (floating point real values)
 complex (complex numbers)

6
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
list1 = ['physics', 'chemistry', 1997,
as a list of comma-separated values 2000]; list2 = [1, 2, 3, 4, 5 ]; list3 =
(items) between square brackets. ["a", "b", "c", "d"]
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
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 tup1 = ('physics', 'chemistry',
objects which ordered 1997, 2000);
tup2 = (1, 2, 3, 4, 5 );
and immutable. tup3 = "a", "b", "c", "d";
 The differences between
tuples and lists are, the
tuples cannot be
changed unlike lists and
tuples use parentheses,
whereas lists use square
brackets.

16
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
dict = {'Name': 'Zara', 'Age': 7, 'Name':
 More than one entry per key 'Manni'}
not allowed. Which means print "dict['Name']: ", dict['Name']
no duplicate key is allowed.
 Keys must be immutable.
dict = {['Name']: 'Zara', 'Age': 7} print
"dict['Name']: ", dict['Name']

21
Dictionaries
 Built-in Dictionary Functions & Methods

22
Dictionaries
 Dictionary
methods

23

You might also like