Download Python Programming: Getting Started with Input, Output, and Variables and more Lecture notes Software Engineering in PDF only on Docsity!
Chapter 1
Getting Started
1. A first program
Go to the IDLE and open a new window (select New Window under the File Menu). Then => Type in the following code. Then, under the Run menu, choose Run Module Note : 1.the IDLE will ask you to save the file, and you should do so.
- Be sure to append .py to the filename as IDLE will not automatically append it. When you’ve saved the program, it will run in the shell window. The program will ask you for a Enter number. Then enter any number. The program’s output looks something like this: Let us study how this the program works. In line number 1 line we have : number = eval(input( 'Enter a Number: ' )) The input function requests the user to enter a number in. The purpose of quotes ( 'Enter a Number: ' ) to show what the user should input. However, the input function will treat 1# number = eval(input( 'Enter a Number: ' )) 2# print( 'Square Number of ' , number, '=' , number*number) Enter a Number: 5 Square Number of 5 = 25 Process finished with exit code 0
any input by default as string, but if we need input a number, we need use another function called eval. Each input value should store in variable, that’s why we need to give name of this value. This variable just memory location that used by our program. In our previous example this variable is a number that hold the value that entered by user. In line #2 , we have 2# print( 'Square Number of ' , number, '=' , numbernumber) The print function to print out the information that can help user to deal with program. The part in quotes is will appear to your program’s user exactly. The argument to the print function is the print the number that user entered, then print “=” , last print the numerical result of calculation numbernumber. Note: This above print function has 4 arguments
2. Input Function
As we explained previous that the input function is a basic way to get user input Here is an example: The output will be Figure 2 print function with 4 arguments 1# name = input ( 'Enter your name: ' )) 2# print( ' Hello ' , name) Enter your name: Bill Hello Bill Process finished with exit code 0
Variable_name =
input (message)
Figure 1 The basic syntax of input function
output will be the output will be Note: In Python, print, Print, and PRINT are all different. Most of keyword in python with lowercase
3.1 Optional arguments
print() has a few extra opinions( called parameters or argument ) which allow to control over the
output of printing.
3.1.1 sep argument
Python by default will insert a space separating between multiple arguments of the print
function. However, There is an optional argument called sep , stand for separator, that we can
adding specific separation instead of default single space
The sum of 2 + 3 = 5 Process finished with exit code 0 num1= num2= print("The sum of ", num1, '+' , num2,'=',num1+num2)
the output will be
use to change that space to character. For instance , using sep='*' would separate the arguments
3.1.2 end argument
The default print function will end the print function to the next line which mean next
printing will display to new line and a newline character is appended. The end argument of print
function will set the string that wants to be appended when printing is done.
the output will be
or we can let end add character then appended
the output will be name= "Bill" Age= 22 print( "name =" ,name , "Age=" ,Age, "Year =" , 2020 ) print( "name =" ,name , "Age=" ,Age, "Year =" , 2020 , sep= " * " ) name = Bill Age= 22 Year = 2020 name = * Bill * Age= * 22 * Year = * 2020 Process finished with exit code 0 Yourname= "BillSmith" print( "Your email" ,Yourname, end= "" ) print( "@MySchool.edu " )
Yourname= "BillSmith" print( "Your email" ,Yourname, end= "@" ) print( "MySchool.edu " )
Conversion Type
The conversion type, , is the last component of the conversion specifier:
%[][][.]
It determines the type of conversion the corresponding value undergoes before insertion into the
format string. Here’s a table that lists the possible conversion types:
Conversion Type
d, i, u Decimal integer
x, X Hexadecimal integer
o Octal integer
f, F Floating point
e, E Exponential
g, G Floating point or Exponential
c Single character
s, r, a String
% Single '%' character
bound of Apple cost $1.740 USD
3.3 lines: Input, assignment
4. Variables
In previous our first program example we have used of a variable called number:
The main purposes of a variable are used to store information to manipulate a value so that it can be
used in another part of the program. Also, it is a way of labeling data with a descriptive name, so
another developer or user of our programs can be easily understood ours code.
In the case above, the variable number stores the value that the user enters so that we can do a
calculation with it in the next line.
Variable names
There are just a some of rules we need take it in our consideration when we are labeling your variables.
- Variable names can contain letters, numbers, and the underscore.
- Variable names cannot contain spaces.
- Variable names cannot start with a number.
- Case matters—for instance, number and Number are different variables.
Exercises
1- Write a program that Ask the user to enter three numbers then computes and prints the result of average of this number.
Reference
https://realpython.com/python-input-output/
1# number = eval(input( 'Enter a Number: ' )) 2# print( 'Square Number of ' , number, '=' , number*number)