Unit 1
Unit 1
Unit 1
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 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"
x=5
y = "John"
print(x, y)