Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Module-1

The document discusses Python programming and provides details about what programming is, why one should learn to program, what Python is, why Python, variables in Python, data types, operators, functions, control flow statements and more. Python is a high-level, general-purpose programming language that is simple, powerful and can be used across many platforms.

Uploaded by

umag7075
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Module-1

The document discusses Python programming and provides details about what programming is, why one should learn to program, what Python is, why Python, variables in Python, data types, operators, functions, control flow statements and more. Python is a high-level, general-purpose programming language that is simple, powerful and can be used across many platforms.

Uploaded by

umag7075
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Python Programming

S Savitha
Asst. Professor
WHAT IS PROGRAMMING

Programming is simply the act of entering instructions for the


computer to perform.
A program is a sequence of instructions intended to perform some
task.
WHY SHOULD YOU LEARN TO WRITE PROGRAMS

“Everybody in this country should learn how to program a


computer… because it teaches you how to think”
–Steve Jobs
● Attractive salary & multiple career opportunities
● Develops problem solving skills and logical skills
● Develop interpersonal skills
● Technologies Are Ruling the World
● Coding is Creativity
What is Python?

Python, a programming language that was brought into existence


by the brilliant mind of Guido van Rossum, made its grand
entrance into the world in the year 1991.
The name python comes from the surreal British comedy group
“Monty Python”, not from the snake.
Python is a remarkable combination of being open-source,
high-level, and general-purpose
WHY PYTHON?
● Python’s Simplicity and Readability
● The clear and beautiful syntax of Python is one of its most notable qualities
● 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.
APPLICATIONS

1. Python for Data Analysis

2. Python for Data Visualization

3. Python for Machine Learning

4. Python for Software Development

5. Python for Web Development

6. Python for Task Automation/Scripting


VARIABLE

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Variables are case sensitive.

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.


Techniques to give variable names
Camel Case
Each word, except the first, starts with a capital letter: myVariableName =
"John"
Pascal Case
Each word starts with a capital letter: MyVariableName = "John"
Snake Case
Each word is separated by an underscore character:
my_variable_name = "John"
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
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.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)

x = y = z = "Orange"
print(x)
print(y)
print(z)
EXAMPLE

x = 5
y = "John"
print(type(x))
print(type(y))
<class 'int'>
<class 'str'>

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

<class 'int'>
<class 'str'>
Blocks of code

Lines of Python code can be grouped together in blocks. You can tell when a block
begins and ends from the indentation of the lines of code.

Rules for blocks:

● Blocks begin when the indentation increases.


● Blocks can contain other blocks.
● Blocks ends when the indentation decreases.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability
only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
EX:
if 5 > 2:
print("Five is greater than two!")
Syntax error
if 5 > 2:
print("Five is greater than two!")
The number of spaces is up to you as a programmer, the most common use is
four, but it has to be at least one.

if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
Syntax error:

if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Python Comments

Comments starts with a #


#This is a comment
print("Hello, World!")
you can add a multiline string (triple quotes) in your code, and place your
comment inside it
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Arithmetic operator
Division operator

Float division: The quotient returned by this operator is always a float number, no
matter if two numbers are integers. For example:

Example: The code performs division operations and prints the results. It
demonstrates that both integer and floating-point divisions return accurate results.
print(5/5) #1.0
print(10/2)#5.0
print(-10/2)#-5.0
print(20.0/2)#10.0
Division operator

Floor division/ Integer division: The quotient returned by this operator is dependent
on the argument being passed. If any of the numbers is float, it returns output in
float.
It is also known as Floor division because, if any number is negative, then the
output will be floored.
print(10//3)#3
print(10.0//3)#3.0
print (-5//2)#-3
print (5.0//2)#2.0
print (-5.0//2)#-3.0
Assignment operator
Comparator operator
Logical operator
Membership operator
Bit operator
Order of Operations

The Python operators follow the precedence rule (which can be remembered as
PEMDAS)

Parenthesis, Exponentiation, Multiplication and Division, Addition and Subtraction

next priority: Out of these two operations, whichever comes first in the expression
is evaluated.
function input()
Python uses the built-in function input() to read the data from the keyboard. When
this function is invoked, the user-input is expected.

For example:

>>> str1=input()

Hello how are you? #user input

>>> print(“String is “,str1)

String is Hello how are you? #printing str1

One can use new-line character \n in the function input() to make the cursor to
appear in the next line of prompt message
float()
A function float() is used to convert a valid value enclosed within quotes into float
number as shown below -
>>> f= input("Enter a float value:")
Enter a float value: 3.5
>>> type(f)
<class 'str'> #f is actually a string “3.5”
>>> f= float(f) #converting “3.5” into float value 3.5
>>> type(f)
<class 'float'>
chr()

A function chr() is used to convert an integer input into equivalent ASCII


character.

>>> a=int(input("Enter an integer:"))

Enter an integer:65

>>> ch=chr(a)

>>> print("Character Equivalent of ", a, "is ",ch)

Character Equivalent of 65 is A
Flow control statements

● If statements
● Else statements
● Elif statements
● While loop statements
If statements

if condition:

Statement block

>>> x=10
>>> if x<40:
print("Fail")

Fail #output
If-else (Alternative Execution)

x=int(input("Enter x:"))
if x%2==0:
print("x is even")
else:
print("x is odd")
Chained Conditionals

The conditions are checked one by one sequentially. If any condition is satisfied, the
The conditions are checked one by one sequentially. If any condition is satisfied, the
respective statement block will be executed and further conditions are not checked.
Note that, the last else block is not necessary always.
Nested Conditionals
The conditional statements can be nested. That is, one set of conditional statements can
be nested inside the other. It can be done in multiple ways depending on programmer’s
requirements.
Ex: marks=float(input("Enter marks:"))
if marks>=60:
if marks<70:
print("First Class")
else:
print("Distinction")
gender=input("Enter gender:")
age=int(input("Enter age:"))
if gender == "M" :
if age >= 21:
print("Boy, Eligible for Marriage")
else:
print("Boy, Not Eligible for Marriage")

elif gender == "F":


if age >= 18:
print("Girl, Eligible for Marriage")
else:
print("Girl, Not Eligible for Marriage")

You might also like