3  Sample Programs

Pyodide Status

Initializing Python Packages

3.1 Introduction to Python

In this tutorial, we will cover the basics of Python programming, including data types, keywords, variables, input/output statements, operators, arithmetic expressions, operator precedence, and evaluation of expressions.

3.2 Data Types

Python supports several built-in data types. Let’s explore some of the most common ones:

  • Integer (int): Represents whole numbers.
  • Floating Point (float): Represents decimal numbers.
  • String (str): Represents sequences of characters.
  • Boolean (bool): Represents True or False.

Example 1

# Demonstrating different data types

# Integer
a = 10
print("Integer:", a, type(a))

# Float
b = 3.14
print("Float:", b, type(b))


# String
c = "Hello, Python!"
print("String:", c, type(c))

# Boolean
d = True
print("Boolean:", d, type(d))
Integer: 10 <class 'int'>
Float: 3.14 <class 'float'>
String: Hello, Python! <class 'str'>
Boolean: True <class 'bool'>

3.2.1 Variables

Variables are used to store data in memory. A variable is created when you assign a value to it using the = operator.

3.2.2 Input and Output Statements

Python provides the input() function to take user input and the print() function to display output.

3.2.3 Operators

Operators are special symbols used to perform operations on variables and values. Python supports several types of operators:

Arithmetic Operators: +, -, *, /, //, %, ** Comparison Operators: ==, !=, >, <, >=, <= Logical Operators: and, or, not Assignment Operators: =, +=, -=, *=, /=, //=, %=, **=

3.2.4 Arithmetic Expressions

An arithmetic expression is a combination of numbers, operators, and variables that evaluates to a value.

3.2.5 Operator Precedence

Operator precedence determines the order in which operations are performed in an expression. The following list shows the precedence from highest to lowest:

** (Exponentiation) *, /, //, % (Multiplication, Division, Floor Division, Modulus) +, - (Addition, Subtraction)

3.2.6 Evaluation of Expressions

Python evaluates expressions from left to right, following the precedence rules.

3.2.7 Conditional Statements in Python

Conditional statements in Python allow the execution of specific code blocks based on whether a condition is true or false. Let’s explore various types of conditional statements.

3.2.7.1 The if Statement

The if statement tests a specific condition. If the condition is true, the code block under the if statement is executed.

Example

Explanation

The above program checks if number is greater than 0. Since 10 is greater than 0, the condition is true, and the message is printed.

3.2.7.2 The if-else Statement

The if-else statement allows you to execute one block of code if the condition is true and another block if it is false.

Explanation

In this example, the program checks if number is greater than or equal to 0. If true, it prints that the number is non-negative. Otherwise, it prints that the number is negative.

Example 2

Explanation

This program checks if a person’s age is greater than or equal to 18. If true, it prints that the person is eligible to vote. Otherwise, it states they are not eligible to vote.

3.2.8 The elif Statement

The elif statement, short for “else if,” allows you to check multiple conditions sequentially. If one of the conditions is true, the corresponding block of code is executed.

Explanation

Here, the program checks three conditions: whether the number is positive, zero, or negative. The elif statement handles the case where number is exactly 0.

Explanation

This program assigns a grade based on the marks obtained. Depending on the range in which the marks fall, the corresponding grade is assigned and printed.

3.2.8.1 Nested if-else Statements

Nested if-else statements allow you to include an if-else statement inside another if-else block for handling more complex conditions.

Explanation

This example checks if a number is positive and then further checks whether it is even or odd using nested if-else statements.

Explanation

This program checks if a score is at least 50. If true, it further checks if the score is 90 or above, printing “Excellent!” if it is, and “Good job!” if it isn’t. If the score is below 50, it prints “Better luck next time.”