Unit 1
Unit 1
Comments
● Comments are the additional readable information to get better understanding about the
source code.
● Comments in Python are the non-executable statements.
● Comments are of 2 types:
o 1)Single Line Comments: which begin with a hash symbol (#).
o E.g. #This is a sample python program
o 2)Multiline Comments: which begins with ‘’’ and ends with ‘’’(3 single quotes)
o E.g. ‘’’This is a sample python program1
This is a sample python program2 ’’’
Expression:
● An expression is any legal combination of symbols that represents a value.
● An expression represents something which python evaluates and which produces a value.
● E.g. 10, x + 5
Statements:
● A statement is a programming instruction that does something i.e. some action takes place.
● A statement executes and may or may not results in a value.
● E.g. print(x + 2), y = x + 5, x = 10
Functions:
● A function is a code that has a name and it can be reused (executed again) by specifying its
name in the program, where needed.
● A function begin with ‘def’ statement
● E.g. goinggood( )
2.2Python Variables:
Variables:
● Variables are containers for storing data values.
Rules for Python Variable:
● A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume).
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different variables).
Valid Variable Name: Invalid Variable Name:
myvar = "John" 2myvar = "John"
my_var = "John" my-var = "John"
_my_var = "John" my var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Creating Variables
● Python has no command for declaring a variable.
● A variable is created the moment you first assign a value to it.
● Variables do not need to be declared with any particular type, and can even change type after
they have been set.
Example:
Note: String variables can be declared either by using single or double quotes.
Other Examples:
Example Data Type
x = str("Hello World") str
x = int(20) int
x = float(20.5) float
x = complex(1j) Complex
x = bool(5) bool
Python Casting
● There may be times when you want to specify a type on to a variable. This can be done with
casting. Python is an object-orientated language, and as such it uses classes to define data types,
including its primitive types.
●Casting in python is therefore done using functions as follows:
● int() – an integer number from an integer literal, a float literal (by removing all decimals),
or a string literal (providing the string represents a whole number)
● float() - a float number from an integer literal, a float literal or a string literal (providing the
string represents a float or an integer)
●str() - a string from a wide variety of data types, including strings, integer literals and float literals
Example:
Example:
Calling a Function
● To call a function, use the function name followed by parenthesis:
Example
Arguments
● Information can be passed into functions as arguments.
● Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
● The following example has a function with one argument (fname). When the function is called,
we pass along a first name, which is used inside the function to print the full name:
Example:
Number of Arguments
● By default, a function must be called with the correct number of arguments. Meaning that if
your function expects 2 arguments, you must call the function with 2 arguments, not more, and
not less.
Example
(This function expects 2 arguments, and gets 2 arguments:)
Global Variables
● Variables that are created outside of a function are known as global variables.
● Global variables can be used by everyone, both inside of functions and outside.
Example:
Note: If you create a variable with the same name inside a function, this variable will be local, and can
only be used inside the function. The global variable with the same name will remain as it was, global
and with the original value.
Example:
print("Python is " + x)
Example
● To change the value of a global variable inside a function, refer to the variable by using the
global keyword:
x = "awesome" Output:
Python is fantastic
def myfunc():
global x
x = "fantastic"
print("Python is " + x)