0% found this document useful (0 votes)
3 views3 pages

Learning Python

The document explains comments in Python, stating they begin with a hash character and are used to clarify code without being interpreted. It also covers variable definition, emphasizing that undefined variables will cause errors, and describes Python's dynamic and strong typing with various number data types. Additionally, it details functions, their purpose for code reuse and procedural decomposition, how to declare and call them, and provides an example of a function that computes the product of two arguments.

Uploaded by

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

Learning Python

The document explains comments in Python, stating they begin with a hash character and are used to clarify code without being interpreted. It also covers variable definition, emphasizing that undefined variables will cause errors, and describes Python's dynamic and strong typing with various number data types. Additionally, it details functions, their purpose for code reuse and procedural decomposition, how to declare and call them, and provides an example of a function that computes the product of two arguments.

Uploaded by

danielmasai033
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

COMMENTS IN PYTHON

 Comments in Python start with the hash character, #, and extend to the end
of the physical line.
 A comment may appear at the start of a line or following whitespace or
code, but not within a string literal.
 A hash character within a string literal is just a hash character.
 Comments are to clarify code and are not interpreted by Python,

# this is the first comment


spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside
quotes."

width = 20
height = 5 * 9
width * height

VARIABLES DEFINITION

NOTE: If a variable is not “defined” (assigned a value), trying to use it will give
you an error.

DATA TYPES

Python is dynamically typed and strongly typed, a constraint that means you can
perform on an object only operations that are valid for its type.
Numbers

Python number data types include: integers that have no fractional part, floating-
point numbers that do, complex numbers with imaginary parts, decimals with
fixed precision, rationals with numerator and denominator, and full-featured sets.

FUNCTIONS

A function is a devise that groups a set of statements so they can be run more than
once in a program – a packaged procedure invoked by name.

Use of functions

1. Maximizing code reuse and minimizing redundancy: functions allow us


to code an operation in a single place and use it in many places which can be
and because of this we reduce code redundancy in our programs and thereby
reduce maintenance efforts.
2. Procedural decomposition: Functions provide a tool for splitting systems
into places that have well-defined roles. By doing so, we make it easier to
implement the smaller tasks in isolation than it is to implement the entire
process at once.

Coding functions

 Declaring a function
A function is declared by the def keyword. A function does not exists until
Python reaches and runs the def.
The def statement creates a function object and assigns it to a name.

def name(arg1, arg2,…. argN): // specifies a function name for the function
statements // Code Python executes each time the function is called
return value

The argument (arg) names in the header are assigned to the objects passed
in parentheses at the point of call.
The return statement exits the function
def func( ):….. # Create a function object
func( ) # Call object
[Link] = value # Attach attributes

Example

# function to compute product and returns the product of its two arguments

Def product(X, y): # Creates and assign function

Return x*y # Body executed when called

# Calling the product function

times = product(2, 4) # arguments in parentheses and saves the product


in times variable

Functions are called in expressions, and are passed values, and return results.

You might also like