KEMBAR78
Introduction_to_Python_operators_datatypes.pptx
A practical introduction to
Python
An introduction to coding with Python
An introductory coding course with Python…
Interpretive vs compiled languages
• Python is an interpretive language.
• This means that your code is not directly run by the hardware. It is
instead passed to a virtual machine, which is just another programme
that reads and interprets your code. If your code used the ‘+’ operation,
this would be recognised by the interpreter at run time, which would
then call its own internal function ‘add(a,b)’, which would then execute the
machine code ‘ADD’.
• This is in contrast to compiled languages, where your code is translated
into native machine instructions, which are then directly executed by the
hardware. Here, the ‘+’ in your code would be translated directly in the
‘ADD’ machine code.
Advantages of Python?
Because Python is an interpretive
language, it has a number of
advantages:
• Automatic memory management.
• Expressivity and syntax that is ‘English’.
• Ease of programming.
• Minimises development time.
• Python also has a focus on importing
modules, a feature that makes it useful
for scientific computing.
Disadvantages
• Interpreted languages are slower than compiled languages.
• The modules that you import are developed in a decentralised
manner; this can cause issues based upon individual assumptions.
• Multi-threading is hard in Python
Which language is the best
• No one language is
better than all others.
• The ‘best’ language
depends on the task
you are using it for and
your personal
preference.
• Introduction to Python Programming
Operators
• Python divides the operators in the following groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
Examples
Relational Operator
Example
Logical Operators
Example
Bit-wise Operator
10&6
10: 00001010
6: 00000110
-------------------
00000010
10|6
10: 00001010
6: 00000110
-------------------
00001110
10^6
10: 00001010
6: 00000110
-------------------
00001100
5<<2
5: 00000101
-------------------
00010100->20
5>>2
5: 00000101
-------------------
000000011
~11
11: 00001011
-------------------
~11:11110100
12’s complement
Variables
• Variables in python can contain alphanumerical characters and some
special characters.
• By convention, it is common to have variable names that start with
lower case letters and have class names beginning with a capital
letter; but you can do whatever you want.
• Some keywords are reserved and cannot be used as variable names
due to them serving an in-built Python function; i.e. and, continue, break.
Your IDE will let you know if you try to use one of these.
• Python is dynamically typed; the type of the variable is derived from
the value it is assigned.
Variable types
• Integer (int)
• Float (float)
• String (str)
• Boolean (bool)
• Complex (complex)
• […]
• User defined (classes)
• A variable is assigned using the = operator; i.e:
In: Out:
• Create an integer, float, and string variable.
• Print these to the screen.
• Play around using different variable names,
etc.
• The print() function is used to print something
to the screen.
• You can always check the type of a variable using the type() function.
In: Out:
• Check the type of one of your variables.
• Variables can be cast to a different type.
In: Out:
Arithmetic operators
The arithmetic operators:
• Addition: +
• Subtract: -
• Multiplication: *
• Division: /
• Power: **
• Write a couple of operations
using the arithmetic operators,
and print the results to the
screen.
In: Out:
A quick note on the increment operator
shorthand
• Python has a common idiom that is not necessary, but which is used frequently and is
therefore worth noting:
x += 1
Is the same as:
x = x + 1
• This also works for other operators:
x += y # adds y to the value of x
x *= y # multiplies x by the value y
x -= y # subtracts y from x
x /= y # divides x by y
Boolean operators
• Boolean operators are useful when making conditional statements,
we will cover these in-depth later.
• and
• or
• not
Comparison operators
• Greater than: >
• Lesser than: <
• Greater than or equal to: >=
• Lesser than or equal to: <=
• Is equal to: ==
In:
Out:
• Write a couple of operations using
comparison operators; i.e.
In: Out:
Working with strings
• Create a string variable.
• Work out the length of the string.
Dictionaries
• Dictionaries are lists of key-valued pairs.
In:
Out:
Indexing
• Indexing in Python is 0-based, meaning that the first element in a
string, list, array, etc, has an index of 0. The second element then has
an index of 1, and so on.
In: Out:
• You can cycle backwards through a list, string, array, etc, by placing a
minus symbol in front of the index location.
In: Out:
In: Out:
In: Out:
• Create a string that is 10 characters in length.
• Print the second character to the screen.
• Print the third to last character to the screen.
• Print all characters after the fourth character.
• Print characters 2-8.
Tuples
• Tuples are containers that are immutable; i.e. their contents cannot
be altered once created.
In: Out:
In: Out:
Lists
• Lists are essentially containers of
arbitrary type.
• They are probably the container
that you will use most frequently.
• The elements of a list can be of
different types.
• The difference between tuples
and lists is in performance; it is
much faster to ‘grab’ an element
stored in a tuple, but lists are
much more versatile.
• Note that lists are denoted by []
and not the () used by tuples.
In:
Out:
• Create a list and populate it with
some elements.
• Lists are mutable; i.e. their contents can be changed. This can be done
in a number of ways.
• With the use of an index to replace a current element with a new one.
In: Out:
Adding elements to a list
• Replace the second element in your string with the integer 2.
• You can use the insert() function in order to add an element to a list at
a specific indexed location, without overwriting any of the original
elements.
In: Out:
• Use insert() to put the integer 3 after the 2 that you just added to your
string.
• You can add an element to the end of a list using the append() function.
In: Out:
• Use append() to add the string “end” as the last element in your list.
Removing elements from a list
• You can remove an element from a list based upon the element value.
• Remember: If there is more than one element with this value, only
the first occurrence will be removed.
In: Out:
• It is better practice to remove elements by their index using the del
function.
In: Out:
• Use del to remove the 3 that you added to the list earlier.
Type Conversion
You can convert from one type to another with the int(), float(),
and complex() methods:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Built-in functions
• Type…help(function)
Ex:
• help(abs)
Help on built-in function abs in module builtins:
abs(x, /)
Return the absolute value of the argument.
• help(pow)
Help on built-in function pow in module builtins:
pow(base, exp, mod=None)
Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
Some common built-in functions for numbers
• abs(x), pow(x,y), min(x1,x2,x3…),max(x1,x2,x3..)
• divmod(x,y),round(x,[n]),bin(x),oct(x),hex(x)
• Ex:
• a=abs(-3)
print(a)
3
• print(min(10,20,30))
10
• print(max(10,20,30))
30
• print(hex(8))
0x8
• print(bin(8))
0b1000
• print(oct(8))
0o10
Built-in Modules
• Python provides many built-in modules. Each module contains many
functions.
• Ex: math, cmath, random,decimal
• To use functions defined in a, we need to import the module using
the import statement.
Example
Example
• Import math
• Print(math.sqrt(36))

Introduction_to_Python_operators_datatypes.pptx

  • 1.
  • 2.
    An introduction tocoding with Python An introductory coding course with Python…
  • 3.
    Interpretive vs compiledlanguages • Python is an interpretive language. • This means that your code is not directly run by the hardware. It is instead passed to a virtual machine, which is just another programme that reads and interprets your code. If your code used the ‘+’ operation, this would be recognised by the interpreter at run time, which would then call its own internal function ‘add(a,b)’, which would then execute the machine code ‘ADD’. • This is in contrast to compiled languages, where your code is translated into native machine instructions, which are then directly executed by the hardware. Here, the ‘+’ in your code would be translated directly in the ‘ADD’ machine code.
  • 4.
    Advantages of Python? BecausePython is an interpretive language, it has a number of advantages: • Automatic memory management. • Expressivity and syntax that is ‘English’. • Ease of programming. • Minimises development time. • Python also has a focus on importing modules, a feature that makes it useful for scientific computing.
  • 5.
    Disadvantages • Interpreted languagesare slower than compiled languages. • The modules that you import are developed in a decentralised manner; this can cause issues based upon individual assumptions. • Multi-threading is hard in Python
  • 6.
    Which language isthe best • No one language is better than all others. • The ‘best’ language depends on the task you are using it for and your personal preference.
  • 7.
    • Introduction toPython Programming
  • 11.
    Operators • Python dividesthe operators in the following groups: • Arithmetic operators • Assignment operators • Comparison operators • Logical operators • Identity operators • Membership operators • Bitwise operators
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    10&6 10: 00001010 6: 00000110 ------------------- 00000010 10|6 10:00001010 6: 00000110 ------------------- 00001110 10^6 10: 00001010 6: 00000110 ------------------- 00001100 5<<2 5: 00000101 ------------------- 00010100->20 5>>2 5: 00000101 ------------------- 000000011 ~11 11: 00001011 ------------------- ~11:11110100 12’s complement
  • 20.
    Variables • Variables inpython can contain alphanumerical characters and some special characters. • By convention, it is common to have variable names that start with lower case letters and have class names beginning with a capital letter; but you can do whatever you want. • Some keywords are reserved and cannot be used as variable names due to them serving an in-built Python function; i.e. and, continue, break. Your IDE will let you know if you try to use one of these. • Python is dynamically typed; the type of the variable is derived from the value it is assigned.
  • 21.
    Variable types • Integer(int) • Float (float) • String (str) • Boolean (bool) • Complex (complex) • […] • User defined (classes) • A variable is assigned using the = operator; i.e: In: Out: • Create an integer, float, and string variable. • Print these to the screen. • Play around using different variable names, etc. • The print() function is used to print something to the screen.
  • 22.
    • You canalways check the type of a variable using the type() function. In: Out: • Check the type of one of your variables.
  • 23.
    • Variables canbe cast to a different type. In: Out:
  • 24.
    Arithmetic operators The arithmeticoperators: • Addition: + • Subtract: - • Multiplication: * • Division: / • Power: ** • Write a couple of operations using the arithmetic operators, and print the results to the screen. In: Out:
  • 25.
    A quick noteon the increment operator shorthand • Python has a common idiom that is not necessary, but which is used frequently and is therefore worth noting: x += 1 Is the same as: x = x + 1 • This also works for other operators: x += y # adds y to the value of x x *= y # multiplies x by the value y x -= y # subtracts y from x x /= y # divides x by y
  • 26.
    Boolean operators • Booleanoperators are useful when making conditional statements, we will cover these in-depth later. • and • or • not
  • 27.
    Comparison operators • Greaterthan: > • Lesser than: < • Greater than or equal to: >= • Lesser than or equal to: <= • Is equal to: == In: Out: • Write a couple of operations using comparison operators; i.e.
  • 28.
    In: Out: Working withstrings • Create a string variable. • Work out the length of the string.
  • 29.
    Dictionaries • Dictionaries arelists of key-valued pairs. In: Out:
  • 30.
    Indexing • Indexing inPython is 0-based, meaning that the first element in a string, list, array, etc, has an index of 0. The second element then has an index of 1, and so on. In: Out: • You can cycle backwards through a list, string, array, etc, by placing a minus symbol in front of the index location. In: Out:
  • 31.
    In: Out: In: Out: •Create a string that is 10 characters in length. • Print the second character to the screen. • Print the third to last character to the screen. • Print all characters after the fourth character. • Print characters 2-8.
  • 32.
    Tuples • Tuples arecontainers that are immutable; i.e. their contents cannot be altered once created. In: Out: In: Out:
  • 33.
    Lists • Lists areessentially containers of arbitrary type. • They are probably the container that you will use most frequently. • The elements of a list can be of different types. • The difference between tuples and lists is in performance; it is much faster to ‘grab’ an element stored in a tuple, but lists are much more versatile. • Note that lists are denoted by [] and not the () used by tuples. In: Out: • Create a list and populate it with some elements.
  • 34.
    • Lists aremutable; i.e. their contents can be changed. This can be done in a number of ways. • With the use of an index to replace a current element with a new one. In: Out: Adding elements to a list • Replace the second element in your string with the integer 2.
  • 35.
    • You canuse the insert() function in order to add an element to a list at a specific indexed location, without overwriting any of the original elements. In: Out: • Use insert() to put the integer 3 after the 2 that you just added to your string.
  • 36.
    • You canadd an element to the end of a list using the append() function. In: Out: • Use append() to add the string “end” as the last element in your list.
  • 37.
    Removing elements froma list • You can remove an element from a list based upon the element value. • Remember: If there is more than one element with this value, only the first occurrence will be removed. In: Out:
  • 38.
    • It isbetter practice to remove elements by their index using the del function. In: Out: • Use del to remove the 3 that you added to the list earlier.
  • 39.
    Type Conversion You canconvert from one type to another with the int(), float(), and complex() methods: x = 1 # int y = 2.8 # float z = 1j # complex #convert from int to float: a = float(x) #convert from float to int: b = int(y) #convert from int to complex: c = complex(x) print(a) print(b) print(c) print(type(a)) print(type(b)) print(type(c))
  • 40.
    Built-in functions • Type…help(function) Ex: •help(abs) Help on built-in function abs in module builtins: abs(x, /) Return the absolute value of the argument. • help(pow) Help on built-in function pow in module builtins: pow(base, exp, mod=None) Equivalent to base**exp with 2 arguments or base**exp % mod with 3 arguments
  • 41.
    Some common built-infunctions for numbers • abs(x), pow(x,y), min(x1,x2,x3…),max(x1,x2,x3..) • divmod(x,y),round(x,[n]),bin(x),oct(x),hex(x) • Ex: • a=abs(-3) print(a) 3 • print(min(10,20,30)) 10 • print(max(10,20,30)) 30 • print(hex(8)) 0x8 • print(bin(8)) 0b1000 • print(oct(8)) 0o10
  • 42.
    Built-in Modules • Pythonprovides many built-in modules. Each module contains many functions. • Ex: math, cmath, random,decimal • To use functions defined in a, we need to import the module using the import statement.
  • 43.
  • 46.
    Example • Import math •Print(math.sqrt(36))