Nothing Special   »   [go: up one dir, main page]

Unit 1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

INT-108

Python Programming

Divya Thakur
28300
Unit 1:
• Setting Up your Programming Enviornment :
• Python Versions.
• Python On Windows.
• Running a “Hello World “ Program.
Python
• Python is a popular programming language. It was created by Guido van
Rossum, and released in 1991.
• 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 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 versions:

• Guido Van Rossum published the first version of Python code (Python 0.9.0)
at alt.sources in February 1991. This release included exception handling,
functions and the core data types.

• Python 1.0 released in January 1994. It included filters, map, reduce and
lambda.

• Python 2.0 released in October 2000. Features include list comprehension


and garbage collection with reference cycles.

• Python 3.0 released in December 2008. This release removed duplicate


constructs and modules, but it was not backward compatible with previous
versions..
Python On Windows:
• Python IDLE : https://www.python.org/downloads/
• Every Python installation comes with an Integrated Development and Learning Environment,
which you’ll see shortened to IDLE or even IDE.
• These are a class of applications that help you write code more efficiently.
• Python IDLE comes included in Python installations on Windows and Mac.
• Once you’ve installed it, you can then use Python IDLE as an interactive interpreter or as a file
editor.
• An Interactive Interpreter
• The best place to experiment with Python code is in the interactive interpreter, otherwise known
as a shell.
• The shell is a basic Read-Eval-Print Loop (REPL).
• It reads a Python statement, evaluates the result of that statement, and then prints the result on
the screen. Then, it loops back to read the next statement.
A File Editor
• Every programmer needs to be able to edit and save text files.
• Python programs are files with the .py extension that contain lines of Python code.
• Python IDLE gives you the ability to create and edit these files with ease.
• How to Use the Python IDLE Shell:
• The shell is the default mode of operation for Python IDLE.
• When you click on the icon to open the program, the shell is the first thing that you see.
• It is a blank Python interpreter window. You can use it to start interacting with Python
immediately.
• You can test it out with a short line of code: print(“hello world”)
How to Work With Python Files:

• Python IDLE offers a full-fledged file editor, which gives you the ability to write and execute
Python programs from within this program.
• The built-in file editor also includes several features, like code completion and automatic
indentation, that will speed up your coding workflow.
• First, let’s take a look at how to write and execute programs in Python IDLE.
• Opening a File:
• To start a new Python file, select File → New File from the menu bar. This will open a blank file in the editor
• Executing a File:
• When you want to execute a file that you’ve created in IDLE, you should first make sure that it’s saved.
• To execute a file in IDLE, simply press the F5 key on your keyboard.
• You can also select Run → Run Module from the menu bar.
• Either option will restart the Python interpreter and then run the code that you’ve written with a fresh
interpreter.
• The process is the same as when you run python3 -i [filename] in your terminal.
• If you ever need to interrupt the execution of your program, then you can press Ctrl+C in the interpreter
that’s running your code
Running a “Hello World “ Program
• print() :
• The print() function prints the specified message to the screen, or other
standard output device.
• The message can be a string, or any other object, the object will be converted
into a string before written to the screen.

• print (“Hello World”) : this will print hello world on to the screen .
• Python Comments:
• Comments can be used to explain Python code.
• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.
• Comments starts with a #, and Python will ignore them
• Example :
• #This is a comment
• print("Hello, World!")
Python Variables
• Variables are containers for storing data values.
• Creating Variables
• Python has no command for declaring a variable.
• A variable is created the moment you first assign a value to it.
• Example:
• x = 20
• y = "Divya"
• print(x)
• print(y)
• Variables do not need to be declared with any particular type, and can even change type after
they have been set.
• x = 20 # x is of type int
• x = "Divya" # x is now of type str
• print(x)
• Casting:
• If you want to specify the data type of a variable, this can be done with casting.
• x = str(3) # x will be '3'
• y = int(3) # y will be 3
• z = float(3) # z will be 3.0
• Get the Type:
• You can get the data type of a variable with the type() function.
• x = 20
• y = "Divya"
• print(type(x))
• print(type(y))
• Single or Double Quotes?
• x = "Divya"
• # is the same as
• x = 'Divya'
• Case-Sensitive:
• Variable names are case-sensitive.
• Example
• This will create two variables:
• a=4
• A = "Divya"
• #A will not overwrite a
Variable Names
• A variable can have a short name (like x and y) or a more descriptive name
(age, carname, total_volume). Rules for Python variables:
• 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)
• A variable name cannot be any of the Python keywords
Legal Variable Names
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
print(myvar)
print(my_var)
print(_my_var)
print(myVar)
print(MYVAR)
print(myvar2)
Illegal Variable Names
• 2myvar = "John"
• my-var = "John"
• my var = "John"

• #This example will produce an error in the result


Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in one line:
• x, y, z = "Orange", "Banana", "Cherry"
• print(x)
• print(y)
• print(z)
Unpack a Collection
• If you have a collection of values in a list, tuple etc. Python allows you
to extract the values into variables. This is called unpacking.
• Example
• Unpack a list:
• fruits = ["apple", "banana", "cherry"]
• x, y, z = fruits
• print(x)
• print(y)
• print(z)
Output Variables

• The Python print() function is often used to output variables.


• x = "Python is awesome"
• print(x)
• In the print() function, you output multiple variables, separated by a
comma:
• x = "Python"
• y = "is"
• z = "awesome"
• print(x, y, z)
• You can also use the + operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
• For numbers, the + character works as a mathematical operator:
x=5
y = 10
print(x + y)
• The best way to output multiple variables in the print() function is to
separate them with commas, which even support different data
types:

x=5
y = "John"
print(x, y)

You might also like