0% found this document useful (0 votes)
25 views5 pages

03 (Input - Range Function)

sgdfnsgf

Uploaded by

Big Boss
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)
25 views5 pages

03 (Input - Range Function)

sgdfnsgf

Uploaded by

Big Boss
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
You are on page 1/ 5

Input Function / String Indexing / Range Function

Input Function:
The input() function allows user input.

The input() function in Python is used to receive input from the user. It allows
the program to pause and wait for the user to enter some text, and then
captures the user's input as a string.

Syntax
user_input = input("Prompt: ")

Parameter Values
Parameter Description

Prompt A String, representing a default message before the input.

Example:
Use the prompt parameter to write a message before the input:
name = input('Please, enter your name:')
print('Hello, ' + name)
# The input() function always returns the user's input as a string.

Example: output
name = input('Please enter your first name: ')
age = int(input('Please enter your age: '))
print("My name is ", name, "I am ",age,"years old") Please enter your first name: Sally
# Convert string to an integer or float using int() or float() Please enter your age: 20

My name is Sally I am 20 years old

Dr. Khaldun Al-Moghrabi 2023-2024 Dr. Ali Al-Ghonmein


String Indexing:
String indexing in Python is a way to access individual characters within a string.
Strings are sequences of characters, and each character in a string is associated
with a unique index (position). In Python, string indexing starts from 0 for the
first character, and it goes up to the length of the string minus 1.

For example, a schematic diagram of the indices of the string 'foobar' would look
like this:

Example :
text = "Hello, World!"

text[0] refers to the first character, which is "H"

text[1] refers to the second character, which is "e"

text[2] refers to the third character, which is "l"

text[3] refers to the fourth character, which is "l"

text[4] refers to the fifth character, which is "o"

You can also use negative indexing to access characters from the end of the
string:
text[-1] refers to the last character, which is "!" (exclamation mark).

text[-2] refers to the second-to-last character, which is "d"

text[-3] refers to the third-to-last character, which is "l"

Example: → Extract substrings from a string


Course_title = "Machine Learning"

print(Course_title[0]) # Accessing the first character, which is "M"

print(Course_title[5]) # Accessing the sixth character, which is "n"

print(Course_title[0:4])# Accessing the first four characters, "Mach"

print(Course_title[:]) # Accessing the entire string, "Machine Learning"

print(Course_title[2:]) # Accessing from the third character to the end,


"chine Learning"

print(Course_title[:7]) # Accessing the first seven characters, "Machine "

Dr. Khaldun Al-Moghrabi 2023-2024 Dr. Ali Al-Ghonmein


Example:
text = "Welcome to Python"
first_char = text[0] output

second_char = text[1]
last_char = text[-1]
First character: w

print("First character:", first_char) Second character: e

print("Second character:", second_char) Last character: n


print("Last character:", last_char)

Example: → String Length


S='ALI'
print(len(S)) # output: 3
print(len("Hello")) # Output : 5

The len() function in Python is a built-in function that is used to determine the
length or the number of items in an object, such as a string.

Example:
output
text = "Hello, World!"
length = len(text)

The length of the string is:13


print("The length of the string is:", length)

Dr. Khaldun Al-Moghrabi 2023-2024 Dr. Ali Al-Ghonmein


Range Function:
The range() function returns a sequence of numbers, starting from 0 by
default, and increments by 1 (by default), and stops before a specified number.

Syntax
range([start], stop, [step])

• start (optional): The starting value of the range (inclusive). If omitted, it defaults
to 0.
• stop: The end value of the range (exclusive). This is a required parameter.
• step (optional): The step size or the difference between each value in the range.
If omitted, it defaults to 1.
Here are some examples of how to use the range() function:

1- Creating a range with a specified stop value:


my_range = range(5) # Creates a range from 0 to 4

2- Creating a range with a specified start and stop value:


my_range = range(2, 8) # Creates a range from 2 to 7

3- Creating a range with a specified start, stop, and step:


my_range = range(1, 10, 2) # Creates a range with odd numbers from 1 to 9

4- Using a for loop to iterate through a range:


for num in range(5):
print(num)

This code will print numbers from 0 to 4.

Example:
Show the output:

s2=range(3,7,2) #3 5
print(s2[0],s2[1])
'''
generates a sequence of numbers starting from 3 and ending before 7,
with a step size of 2. Therefore,
the numbers in the sequence are 3 and 5.'''

Dr. Khaldun Al-Moghrabi 2023-2024 Dr. Ali Al-Ghonmein


Exercise: Show the output
1- Show the output:

s=range(5)
print(s)
2- Show the output:
s2=range(3,7,1)
print(s2[0],s2[1], s2[2], s2[3])
3- Show the output:
s2=range(3,7)
print(s2[0],s2[1], s2[2], s2[3])
4- Show the output:
s3=range(10,0,-2)
print(s3[0],s3[1],s3[2],s3[3],s3[4])
5- Show the output:
x = range(6)
for n in x:
print(n , end=' ')
6- Show the output:
x = range(3, 6)
for n in x:
print(n , end = ' ')
7- Show the output:
x = range(3, 20, 2)
for n in x:
print(n , end=' ')

Show the output (Python String – slicing)


a="welcome to Python ~ Ali & Khaldun"

print(a[0:1]) # a[0]
print(a[2:10]) # a[2] to a[9]
print(a[11:13]) # a[11] to a[12]
print(a[8:10]) # a[8] to a[9]
print(a[14:24]) # a[14] to a[24]
print(a[5:50]) # a[5] to end
# Negative Indexing
# (start the slice from the end of the string)
print(a[-10:24])
print(a[-16:-14])
print(a[-13:-11])

Dr. Khaldun Al-Moghrabi 2023-2024 Dr. Ali Al-Ghonmein

You might also like