0% found this document useful (0 votes)
5 views6 pages

UnderstandingPython

The document provides an introduction to basic Python programming functions, including print statements, user input, and conditional statements. It includes examples demonstrating how to use the print() function, the input() function, and the if...else statement. Additionally, it emphasizes the importance of indentation in Python and provides examples of adding numbers both with predefined values and user input.

Uploaded by

battina kotirao
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)
5 views6 pages

UnderstandingPython

The document provides an introduction to basic Python programming functions, including print statements, user input, and conditional statements. It includes examples demonstrating how to use the print() function, the input() function, and the if...else statement. Additionally, it emphasizes the importance of indentation in Python and provides examples of adding numbers both with predefined values and user input.

Uploaded by

battina kotirao
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/ 6

Understanding Python- Programming

Dear Class,

Please find the basic functions and how they are used with some examples stated below.

I would suggest you to , use an online python editor or Compiler and practice these programs.

Click on the link for the online compiler

https://www.onlinegdb.com/online_python_compiler

Python Print Statement

print('Good Morning!')

print('It is rainy today')

Python print() with end Parameter

# print with end whitespace

print('Good Morning!', end= ' ')

print('It is rainy today')

Python print() with sep parameter

print('New Year', 2023, 'See you soon!', sep= '. ')

Print Python Variables and Literals

We can also use the print() function to print Python variables. For example,

number = -10.6

name = "Programiz"

# print literals

print(5)

# print variables

print(number)
print(name)

Python Input

While programming, we might want to take the input from the user. In Python, we can use
the input() function.

Syntax of input()

input(prompt)

Here, prompt is the string we wish to display on the screen. It is optional.

Example: Python User Input

# using input() to take user input

num = input('Enter a number: ')

print('You Entered:', num)

print('Data type of num:', type(num))

Run Code

Output

Enter a number: 10

You Entered: 10

Data type of num: <class 'str'>

In the above example, we have used the input() function to take input from the user and stored the
user input in the num variable.

To convert user input into a number we can use int() or float() functions as:

num = int(input('Enter a number: '))

Python if...else Statement

In computer programming, the if statement is a conditional statement. It is used to execute a block of


code only when a specific condition is met. For example,

Suppose we need to assign different grades to students based on their scores.

1. If a student scores above 90, assign grade A


2. If a student scores above 75, assign grade B

3. If a student scores above 65, assign grade C

These conditional tasks can be achieved using the if statement.

Python if Statement

An if statement executes a block of code only when the specified condition is met.

Syntax

if condition:

# body of if statement

Here, condition is a boolean expression, such as number > 5, that evaluates to either True or False.

 If condition evaluates to True, the body of the if statement is executed.

 If condition evaluates to False, the body of the if statement will be skipped from execution.

Let's look at an example.

Working of if Statement

Example: Python if Statement

number = int(input('Enter a number: '))

# check if number is greater than 0

if number > 0:
print(f'{number} is a positive number.')

print('A statement outside the if statement.')

Run Code

Sample Output 1

Enter a number: 10

10 is a positive number.

A statement outside the if statement.

If user enters 10, the condition number > 0 evaluates to True. Therefore, the body of if is executed.

Sample Output 2

Enter a number: -2

A statement outside the if statement.

If user enters -2, the condition number > 0 evaluates to False. Therefore, the body of if is skipped
from execution.

Indentation in Python

Python uses indentation to define a block of code, such as the body of an if statement. For example,

x=1

total = 0

# start of the if statement

if x != 0:

total += x

print(total)

# end of the if statement

print("This is always executed.")

Run Code
Here, the body of if has two statements. We know this because two statements (immediately after if)
start with indentation.

We usually use four spaces for indentation in Python, although any number of spaces works as long
as we are consistent.

You will get an error if you write the above code like this:

# Error code

x=1

total = 0

if x != 0:

total += x

print(total)

Run Code

Here, we haven't used indentation after the if statement. In this case, Python thinks our if statement
is empty, which results in an error.

In the program below, we've used the + operator to add two numbers.

Example 1: Add Two Numbers

# This program adds two numbers

num1 = 1.5

num2 = 6.3

# Add two numbers

sum = num1 + num2

# Display the sum

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))


Run Code

Output

The sum of 1.5 and 6.3 is 7.8

The program below calculates the sum of two numbers entered by the user.

Example 2: Add Two Numbers With User Input

# Store input numbers

num1 = input('Enter first number: ')

num2 = input('Enter second number: ')

# Add two numbers

sum = float(num1) + float(num2)

# Display the sum

print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Run Code

Output

Enter first number: 1.5

Enter second number: 6.3

The sum of 1.5 and 6.3 is 7.8

In this program, we asked the user to enter two numbers and this program displays the sum of two
numbers entered by user.

You might also like