Professional Documents
Culture Documents
Visual Programming (VP) : Lecture # 3 Engr. Tehmina Kalsum
Visual Programming (VP) : Lecture # 3 Engr. Tehmina Kalsum
Lecture # 3
1
This Lecture Content
Python basics
Variables
Types
Arithmetic operators
Boolean logic
Strings
Printing
Input
Lists
Dictionaries
Sets
If Else
Loops
Functions
2
Python introduction
Python was first released in 1991, Since then, it's gone through many
changes
Scalable, object oriented and functional from the beginning
two versions are in use : Python 2.x versions and Python 3.x versions
Python 2 came out in 2000.
Python 3: is a newer version of the Python programming language, released
in December 2008 in order to fix problems that exist in Python 2
It is backward incompatible.
Its syntax is simpler and easily understandable whereas Python 2 syntax is
comparatively difficult to understand.
Many recent developers are creating libraries which you can only use with
Python 3.
any organization who was using Python 2.x version, migrating their project
to 3.x needed lots of changes.
3
Python 2 vs. Python 3 Example Code
4
Variables
store values
Can later be called by the variable name
Variable names are case sensitive and unique
Types
Variables actually have a type, which defines the way it is stored.
The basic types are:
Note: We can't do arithmetic operations on variables of different
types. Therefore make sure that you are always aware of your
variables types
You can find the type of a variable using type(). For example type
type(x).
Casting types
Python offers us a way of converting variables to different types
result?
Arithmetic operations
vs
13 49
Comparison operators
Tru Tru
e e
Another example
Tru Tru
True
e e
Strings
Powerful and flexible in Python
Can be added
Can be multiplied
Can be multiple lines
Strings
Strings: conversion to upper and lower case
28
Input contd..
By default input() function takes the user’s input in a string.
To take the input in the form of int you need to use int() along with the
input function.
Similarly for float, list, tuple etc.
29
Lists
Comma separated
Curly brackets
Dictionary properties
Values are mapped to a key
Values are accessed by their key
Keys are unique and are immutable
Values cannot exist without a key
Dictionaries
Conditional
statement
Executed if answer is True
Executed if answer is
False
If Else example
Indentation matters
elif = else + if which means that the previous statements must be false
for the current one to evaluate to true
For loop
Allows us to iterate over a set amount of variables within a data structure.
During that we can manipulate each item however we want
What if we have much more than 4 items in the list, say, 1000?
For example
A while loop doesn't run for a predefined number of iterations, like a for loop.
Instead, it stops as soon as a given condition becomes true/false.
Break statement
Allows us to go(break) out of a loop preliminary.
Adds a bit of controllability to a while loop.
Usually used with an if.
Can also be used in a for loop.
Activity
How many times are we going to execute the while loop?
Functions
Allow us to package functionality in a nice and readable way
reuse it without writing it again
Make code modular and readable
Rule of thumb - if you are planning on using very similar code
more than once, it may be worthwhile writing it as a reusable
function.
Function declaration
66
References
https://www.stxnext.com/blog/why-migrate-from-python-2-to-python-3/
https://docs.python.org/3/tutorial/index.html
https://www.w3schools.com/python/python_ref_string.asp
https://www.softwaretestinghelp.com/java-vs-python/
67
68