0% found this document useful (0 votes)
2 views

Python MBA

Uploaded by

scharancherry77
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)
2 views

Python MBA

Uploaded by

scharancherry77
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
You are on page 1/ 5

Introduction:

What is Python?

Python is a popular programming language. It was created by Guido van Rossum, and released in
1991.

It is used for:

 web development (server-side),

 software development,

 mathematics,

 system scripting.

What can Python do?

 Python can be used on a server to create web applications.

 Python can be used alongside software to create workflows.

 Python can connect to database systems. It can also read and modify files.

 Python can be used to handle big data and perform complex mathematics.

 Python can be used for rapid prototyping, or for production-ready software development.

Why Python?

 Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).

 Python has a simple syntax similar to the English language.

 Python has syntax that allows developers to write programs with fewer lines than some
other programming languages.

 Python runs on an interpreter system, meaning that code can be executed as soon as it is
written. This means that prototyping can be very quick.

 Python can be treated in a procedural way, an object-oriented way or a functional way.

Python is an easy to learn, powerful programming language. It has efficient high-level data structures
and a simple but effective approach to object-oriented programming. Python’s elegant syntax and
dynamic typing, together with its interpreted nature, make it an ideal language for scripting and
rapid application development in many areas on most platforms.

python is a high-level, general-purpose programming language that can be used to declare complex
instructions for computers (e.g., for working with information and data). It is an open-
source programming language, which means that it is free and continually improved upon by the
Python community.
 Fun Fact: Python is named after British comedy troupe “Monty Python”, because it seemed
like a good idea at the time.

So far you’ve leveraged formal language to give instructions to your computers, such as by writing
syntactically-precise instructions at the command line. Programming in Python will work in a similar
manner: you will write instructions using Python’s special language and syntax, which the computer
will interpret as instructions for how to work with data.

Indeed, Python is an interpreted language, in that the computer (specifically the Python Interpreter)
translates the high-level language into machine language on the fly at the time you run it (“at
runtime”). The interpreter will read and execute one line of code at a time, executing that line before
it even begins to consider the next. This is in contrast with compiled languages (like C or Java) that
has the computer do the translation in one pass, and then only execute the program after the whole
thing is converted to machine language.

 This means that Python is technically a little slower to execute commands than C or Java
because it needs to both translate and execute at once, but not enough that we’ll notice.

While it’s possible to execute Python instructions one at a time, it’s much more common to write
down all of the instruction in a single script so that the computer can execute them all at once.
Python scripts are saved as files with the .py extension, You can author Python scripts in any text
editor (such as VS Code), but we’ll also write and run Python code in an interactive web application
called Jupyter.

Versions

Python was first published by Dutch programmer Guido Van Rossum in 1991. Although the language
is open-source (and so can be contributed to by anyone), Van Rossum still retains a lot of influence
over where the language goes, and is known as Python’s “Benevolent Dictator for Life”.

Version 2.0 of Python was released in 2000, and version 3.0 was released in 2008. However, unlike
with many other programming languages that release new versions, Python 2 and Python 3
are not compatible with one another: Python 2 code usually won’t execute with Python 3
interpreter, and Python 3 code usually won’t execute with a Python 2 interpreter.

Python 3 involved a major restructuring which included a number of “breaking” changes to core
parts of the language. The most noticeable of these are that all text strings support Unicode (non-
English characters), print is a function not a statement, and integers don’t use floor devision (these
concepts are discussed in more detail below).

In short, Python 3 is considered the “current” version, and Python 2 is considered the “legacy”
version (and is not longer supported). However some few external libraries that failed to update to
Python 3.

Comments

Before discussing how to program with Python, we need to talk about a piece of syntax that lets you
comment your code. In programming, comments are bits of text that are not interpreted as
computer instructions—they aren’t code, they’re just notes about the code! Since computer code
can be opaque and difficult to understand, we use comments to help write down the meaning
and purpose of our code. While a computer is able to understand the code, comments are there to
help people understand. This is particularly imporant when someone else will be looking at your
work—whether that person is a collaborator, or is simply a future version of you (e.g., when you
need to come back and fix something and so need to remember what you were even thinking).
Comments should be clear, concise, and helpful—they should provide information that is not
“obvious” from the code itself.

In Python, we mark text as a comment by putting it after the pound/hashtag symbol (#). Everything
from the # until the end of the line is a comment. We put descriptive comments immediately
above the code it describes, but you can also put very short notes at the end of the line of code
(preferably following two spaces):

# Set how many bottles of beer are on the wall

bottles = 99 - 1 # 98

Data Types

In the examples above, we stored numeric values in variables. Python is a dynamically typed
language, which means that we do not need to explicitly state what type of information will be
stored in each variable we create. The Python interpreter is intelligent enough to understand that if
we have code x = 7, then x will contain a numeric value (and so we can do math upon it!)

You can “look up” the type of a particular value by using the type() function:

type(7) # integer

## <class 'int'>

type("Hello") # string

## <class 'str'>

 Type is also referred to as class (i.e., “classification”). Classes are ways of structuring
information and behavior in computer programs. Classes are discussed in more detail later.

There are a number of “basic types” for data in Python. The two most common are:

 Numeric Types: Python has two data types used to represent numbers: int (integer)
for whole numbers like 7 and float for decimals like 3.14 (because the decimal point is
“floating” in the middle).

We can use mathematical operators on numeric data (such as +, -, *, /, etc.). There are also
numerous functions that work on numeric data (such as for calculating sums or averages). You can
mix and match ints and floats in mathematical expressions; the result will be a float if either of the
operands are floats. Division (/) always produces a float; use the “integer division operator” // to
force the result to be a whole number (rounded down).

 Strings: Python uses the str (string) type to store textual data. str values contain strings of
characters, which are the things you type with a keyboard (including digits, spaces,
punctuation, and even line breaks). You specify that some characters are a string by
surrounding them in either single quotes (') or double quotes (").

 # Create a string variable `famous_poet` with the value "Bill Shakespeare"

famous_poet = "Bill Shakespeare"


Note that string literals (the text in quotes) are values just like numeric literals (numbers), so can be
assigned to variables!

o Using “triple-quotes” will allow you to specify a multi-line string:

o message = """Dear Professor,

o My dog ate my homework.

o Thank you,

o A. Student

"""

Strings support the + operator, which performs concatenation (combining two strings together). For
example:

# concatenate 3 strings. The middle string is a space character

full_name = "Bill" + " " + "Shakespeare"

The operands must both be strings. Also notice that concatenating strings doesn’t add any additional
characters such as spaces between words; you need to handle such punctuation yourself!

There are many built-in functions for working with strings, and in fact strings support a variety of
functions you can call on them. See Functions for details.

There are many other data types as well; more will be introduced throughout the remaining
modules.

Helpful tip: because variables can any type of data, it is often useful to name variables based on their
data type. This helps keep the purpose of code clear in your mind, and makes it easier to read and
understand what is happening:

password_num = 12345 # variable name indicates a number, so can do math on it

password_str = "12345" # variable name indicates a string, so can print it

It is possible to convert from one type to another by using a type converter function, which is usually
named after the type you wish to convert to:

int(3.14) # 3 (converted from a float to an int)

int(5.98) # 5 (takes the floor value instead of rounding)

float(6) # 6.0 (converted from an int to a float)

str(2) # "2" (converted from an int to a str)

int("2") # 2 (converted from a str to an int)

int("two") # ValueError! (cannot convert)

This is particularly useful when you wish to include a number in a string:

favorite_number = 12

message = "My favorite number is "+str(favorite_number)


print(message) # My favorite number is 12

You might also like