Programming in Python Week-2 Content
Blocks in Python ● Grouping of sentences for a specific purpose ● Piece of python program that is executed as a unit ● Can be called once or more ● Can invoke other code blocks ● Indentation used to denote block ● All statements with same indentation level - form one block ● If blocks have to be nested, indent further
Scope of Variables ● Defines visibility of a name within a block ● A variable is only available from inside the region it is created. This is called scope. ● Same variable name inside and outside of a function, Python will treat them as two separate variables.
Local vs Global variables ● Scope of global variables - for the entire program ● Scope of local variables - only in the function it is defined ● global keyword is used to assign/change variables inside functions
Comments ● Comments for increasing understandability of code - for yourself & others ● Types of Comments in Python ○ Single-Line ○ Multi-Line
Single-line Comment
Multiline Comments
Data types
Python Datatypes - int & float ● int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point. ● float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. ● Can use type() to get the datatype of a variable/value
Python Datatypes - Boolean ● Represented by 2 values: True and False ● These values are case-sensitive
Basic Operators ● Arithmetic ● Comparison/ Relational ● Assignment ● Logical
Arithmetic operators
Relational operators
Assignment operators
Logical operators
Logical AND operator - and
Logical OR operator - or
Logical NOT operator - not
What are Arrays?
Strings & Accessing characters
What are keywords? ● Reserved words in Python ● Cannot use a keyword as a variable name, function name ● Keywords are case-sensitive ● If you try to use them as variables, you will end up with SyntaxError ● This can be used to get the keyword help("keywords")
Creating functions in Python Starting a for-loop
global keyword ● Used if you want to make a change to the global variable inside a function. ● If you want to create a global variable from inside a function.
Homework Questions

Programming in python - Week 2

Editor's Notes

  • #3 All the lines will be executed one after the other when a block is invoked. Grouping related code makes it easier to understand