0% found this document useful (0 votes)
2 views38 pages

Print User Input Without Spaces

The document covers Python programming basics, focusing on type conversions, binary numbers, and string formatting. It explains implicit and explicit type conversions, how to handle user input, and the use of formatted string literals (f-strings) for output. Additionally, it includes activities and examples to reinforce the concepts discussed.

Uploaded by

gangareddy368
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views38 pages

Print User Input Without Spaces

The document covers Python programming basics, focusing on type conversions, binary numbers, and string formatting. It explains implicit and explicit type conversions, how to handle user input, and the use of formatted string literals (f-strings) for output. Additionally, it includes activities and examples to reinforce the concepts discussed.

Uploaded by

gangareddy368
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Python programming

20CS4AC07
by
Prof. Roopashree S

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


(Artificial Intelligence and Machine Learning) & (Cybersecurity)
Module-01
Python basics
by
Prof. Roopashree S

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


(Artificial Intelligence and Machine Learning) & (Cybersecurity)
Type conversions

3 20CS3CY01 | Python programming | Prof. Roopashree S


Type conversions

A calculation sometimes must mix integer and floating-point numbers.

For example, given that about 50.4% of human births are males, then 0.504 *
num_births calculates the number of expected males in num_births births.

If num_births is an integer type, then the expression combines a floating-point and


integer.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

A type conversion is a conversion of one type to another, such as an int to a float.

An implicit conversion is a type conversion automatically made by the interpreter,


usually between numeric types. For example, the result of an arithmetic operation like +
or * will be a float only if either operand of the operation is a float.

• 1 + 2 returns an integer type.


• 1 + 2.0 returns a float type.
• 1.0 + 2.0 returns a float type.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

int-to-float conversion is straightforward: 25 becomes 25.0.

float-to-int conversion just drops the fraction: 4.9 becomes 4.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Implicit conversions between float and int.

Type the value held in the variable after the assignment statement, given:

num_items = 5
item_weight = 0.5
For any floating-point answer, type answer to tenths. Ex: 8.0, 6.5, or 0.1

1. num_items + num_items
10

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Implicit conversions between float and int.

2. (num_items + num_items) * item_weight


5.0
A float is included in the expression, so the result will be a float.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Conversion functions

Sometimes a programmer needs to explicitly convert an item's type.

Conversion functions for some common types.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Conversion functions

Converting a float to an int will truncate the floating-point number's fraction.

For example, the variable temperature might have a value of 18.75232, but can be
converted to an integer expression int(temperature).

The result would have the value 18, with the fractional part removed.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Conversion functions

Conversion of types is very common. In fact, all user input obtained using input() is
initially a string and a programmer must explicitly convert the input to a numeric type.

Strings can also be converted to numeric types, if the strings follow the correct
formatting, i.e. using only numbers and possibly a decimal point.

For example, int('500') yields an integer with a value of 500, and float('1.75') yields the
floating-point value 1.75.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Simple example of converting float and int types

Run the below program. Observe how the type conversion affects the entered number.
Change the input to 18.552 and run the program again.

input_text = input('Enter a number:\n’)


Enter a number:
float_variable = float(input_text)
original input text: 18
int_variable = int(float_variable)
input text converted to a float: 18.0
print('original input text:', input_text)
float variable converted to an int: 18
print('input text converted to a float:', float_variable)
print('float variable converted to an int:', int_variable)

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Type conversions-activities

What is the result of each expression?

1. int(1.55)
1

2. float("7.99")
7.99 (float() converts the value directly to floating-point.)

3. str(99)
’99’ (str() converts the value to a string.

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Type conversions-activities
Type casting: Computing average owls per zoo.
Assign avg_owls with the average owls per zoo. Print avg_owls as an [Link](1.55)
Sample output for inputs: 1 2 4
Average owls per zoo: 2

avg_owls = 0.0
num_owls_zooA = int(input())
num_owls_zooB = int(input())
num_owls_zooC = int(input())
''' Your solution goes here ‘‘’
print('Average owls per zoo:', int(avg_owls))

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Type conversions-activities
Type casting: Computing average owls per zoo.
Assign avg_owls with the average owls per zoo. Print avg_owls as an [Link](1.55)
Sample output for inputs: 1 2 4
Average owls per zoo: 2

avg_owls = 0.0
num_owls_zooA = int(input())
num_owls_zooB = int(input())
num_owls_zooC = int(input())
avg_owls = (num_owls_zooA + num_owls_zooB + num_owls_zooC) / 3
print('Average owls per zoo:', int(avg_owls))

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Type conversions-activities
Type casting: Reading and adding values.
Assign total_owls with the sum of num_owls_A and num_owls_B.
Sample output with inputs: 3 4
Number of owls: 7
total_owls = 0
num_owls_A = input()
num_owls_B = input()

''' Your solution goes here ‘‘’

print('Number of owls:', total_owls)

20CS4AC07 | Python programming | Prof. Roopashree S


Type conversions

Type conversions-activities
Type casting: Reading and adding values.
Assign total_owls with the sum of num_owls_A and num_owls_B.
Sample output with inputs: 3 4
Number of owls: 7
total_owls = 0
num_owls_A = input()
num_owls_B = input()

total_owls = int(num_owls_A) + int(num_owls_B)


print('Number of owls:', total_owls)

20CS4AC07 | Python programming | Prof. Roopashree S


Binary numbers

18 20CS3CY01 | Python programming | Prof. Roopashree S


Binary numbers

Binary numbers
Normally, a programmer can think in terms of base ten numbers.

However, a computer must allocate some finite quantity of bits (e.g., 32 bits) for a variable, and
that quantity of bits limits the range of numbers that the variable can represent.

Python allocates additional memory to accommodate numbers of very large sizes (past a typical
32 or 64 bit size), and a Python programmer need not think of such low level details.

However, binary base computation is a common and important part of computer science, so
some background on how the quantity of bits influences a variable's number range is helpful.

20CS4AC07 | Python programming | Prof. Roopashree S


Binary numbers

Binary numbers

Because each memory location is composed of bits (0s and 1s), a processor stores a
number using base 2, known as a binary number.

For a number in the more familiar base 10, known as a decimal number, each
digit must be 0-9 and each digit's place is weighed by increasing powers of 10.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

21 20CS3CY01 | Python programming | Prof. Roopashree S


String formatting

Formatted string literals (f-strings)

Program output commonly includes expressions, like variables or other


calculations, as a part of the output text.

A formatted string literal, or f-string, allows a programmer to create a string


with placeholder expressions that are evaluated as the program executes.

An f-string starts with a f character before the starting quote, and uses curly
braces { } to denote the placeholder expressions.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Formatted string literals (f-strings)


A placeholder expression is also called a replacement field, as its value replaces
the expression in the final output.
Creating literal strings with embedded expressions.
number = 6
amount = 32
print(f'{number} burritos cost ${amount}’)
Output: 6 burritos cost $32
The first expression, {number}, is replaced with the value of the number variable.
The second expression, {amount}, is replaced with the value of the number amount.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Formatted string literals (f-strings)

Identify the output of f-strings.


Select the option that correctly prints the given output. Assume the
following code is defined.
num_items = 3
cost_taco = 1.25
1.I need 3 items please
2.3 tacos cost 3.75

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Additional f-string features

An = sign can be provided after the expression in a replacement field to print


both the expression and its result which is a useful debugging technique when
dynamically generating lots of strings and output.

Ex: f'{2*4=}' produces the string "2*4=8".

Additionally, double braces {{ and }} can be used to place an actual curly brace
into an f-string. Ex: f'{{Jeff Bezos}}: Amazon' produces the string "{Jeff
Bezos}: Amazon".
20CS4AC07 | Python programming | Prof. Roopashree S
String formatting

f-string examples

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

f-string examples

Enter the final value of output.

1. output = f'{2*2}’ #Output is 4

2. output = f'{2*2=}’ #Output is 2*2=4

3. kids = 4
adults = 2
output = f'{kids+adults} total’ #Output is 6 total

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

f-string examples
Enter the final value of output.

4. kids = 4
adults = 2
output = f'{kids+adults=} total’ #Output:kids+adults=6 total

5. output = f'{{2}} + {{3}} = {{5}}' #Output is {2}+{3}={5}

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Format specifications

A format specification inside a replacement field allows a value's formatting in


the string to be customized.

Ex: Using a format specification, a variable with the integer value 4 can be
output as a floating-point number (4.0), with leading zeros (004), aligned to the
left or right, etc.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Format specifications

A format specification is introduced with a colon : in the replacement field.

The colon separates the "what" on the left from the "how" on the right.

The left "what" side is an expression to be evaluated, perhaps just a variable


name or a value.

The right "how" side is the format specification that determines how to show
that value using special characters. Ex: {4:.2f} formats 4 as 4.00.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Format specifications

A presentation type is a part of a format specification that determines how to


represent a value in text form, such as integer (4), floating point (4.0), fixed
precision decimal (4.000), percentage (4%), binary (100), etc.

A presentation type can be set in a replacement field by inserting a colon : and


providing one of the presentation type characters

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Common format specification presentation types

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Activities: Format specifications and presentation types

Enter the most appropriate format specification to produce the desired output.

The value of num as a decimal (base 10) integer: 31

num = 31
print(f'{num:d}’)

num is already defined as a decimal integer 31 in the code, so the decimal presentation
type "d" just prints out an unchanged value.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Activities: Format specifications and presentation types

Enter the most appropriate format specification to produce the desired output.

The value of num as a hexadecimal (base 16) integer: 1f

num = 31
print('{num: }')

The lowercase hexadecimal presentation type "x" outputs decimal value 31 as hexadecimal
value 1f. Note that the value of num does not change, only the representation of num in the
output string is altered.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Activities: Format specifications and presentation types

Enter the most appropriate format specification to produce the desired output.

The value of num as a binary (base 2) integer: 11111

num = 31
print('{num: }')

The binary presentation type "b" outputs decimal value 31 as binary value 11111.

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Activities: Printing an f-string.

Write a single statement to print: user_word,user_number. Note that there is no space between the
comma and user_number.

Sample output with inputs: Amy 5


Amy,5

user_word = input()
user_number = int(input())

''' Your solution goes here '''

20CS4AC07 | Python programming | Prof. Roopashree S


String formatting

Activities: Printing an f-string.

Write a single statement to print: user_word,user_number. Note that there is no space between the
comma and user_number.

Sample output with inputs: Amy 5


Amy,5

user_word = input()
user_number = int(input())

print(f'{user_word},{user_number}')

20CS4AC07 | Python programming | Prof. Roopashree S


Python programming

THANK
YOU

20CS4AC07 | Python programming | Prof. Roopashree S

You might also like