0% found this document useful (0 votes)
3 views

Python

The document provides an overview of Python programming, highlighting its case sensitivity, modes of operation (interactive and script), and the use of variables to store data. It explains data types, casting, and conditional statements, along with examples of input handling and exercises for practice. Additionally, it includes tasks for correcting errors in Python code snippets.

Uploaded by

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

Python

The document provides an overview of Python programming, highlighting its case sensitivity, modes of operation (interactive and script), and the use of variables to store data. It explains data types, casting, and conditional statements, along with examples of input handling and exercises for practice. Additionally, it includes tasks for correcting errors in Python code snippets.

Uploaded by

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

Using Python.


Python is a text based programming language.

Python is case sensitive.

Python Code
Python Interpreter Machine Code
(Source Code)

Interpreter – Translates the source code into language that the computer can understand.

Machine Code – The language that a computer uses to carry out instructions.

There are two modes in Python.



Interactive mode – The Python shell allows commands to be entered and run
immediately.

Script mode – Python’s text editor, which allows programmers to enter a list
of commands and they are executed together.

Python shell – The Python interactive mode, where commands can be typed directly.
Variables

Variable : A named memory location used to store data of a given type
during program execution. A variable can change value as the program runs.

A variable is like a container that stores data in a program.

Programmers give variables specific names to easily refer them and
manipulate data.

user_name = "Joe"
user_age = 25

print("Your age is:", user_age)

user_age = user_age + 1

print("Next year, ",user_name," will be: ", user_age)


Input

Input() : Used to take input from a user. If you wish to store this input you
need to assign it to a variable.

name = input(“What is your name ? ”)


Print (“Hello “, name)

Exercise : Re-write this program utilizing input() command prompting user to input name and age.

user_name = "Joe"
user_age = 25

print("Hello , ",user_name," you are : ", user_age,” years old”)


Data Types.

Data type refers to the type of value a variable has, e.g. integer, string.

String – Any textual characters, such as ‘Hello’, ‘We56’

Integer – Any whole number, such as 24 or -10

Real/Float – Any number with decimal point, such as 6.76 or -0.342

In Python, the data type real is referred as float.

Python code below shows a number of variables storing different pieces of data with different data
types.

name = “Joe”
age = 12
address = “10/H , New Road”
balance = 120.45
Casting

Casting : The process of ensuring data is set to the correct data type.

Python’s input function captures user input as a string data type.

Casting a string to an integer.

age = int(input(“Enter your age :“))

casting a string to a float.

balance = float(input(“Enter how much money left :”))


Conditions – If/Then/Else

number1 = 15 number1 = int(input(“Enter 1st number:”))


number2 = 25 number2 = int(input(“Enter 2nd number:”))
if number1 > number2: if number1 == number2:
print (number1) print (“Numbers are equal”)
else: else:
print (number2) print (“Numbers are not equal ”)
Exercise

Write the solution for the above flowchart using Python.


2) The following Python program has few errors. Identify and correct them to make the code functional.
>> print(“Enter your age :”)
>> age = input
>> print(“You are “+ age + “ years old.”)

3) Rewrite the following Python code to correct any errors:


>> number = input(“Enter a number: “)
>> print(“Double the number is : “ + number * 2 )

You might also like