Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 4 out of 117 pages
Class notes

Full notes for Introduction to Scripting

Document preview thumbnail
Preview 4 out of 117 pages

Everything you need from start to finish, definitions, notes, key concepts explained, examples with actual code and correction code, pseudocode, all in order from chapter 1 to final

Content preview

String length and indexing

A common operation is to find the length, or the umber of characters, in a
string. The len() built-in function can be used to find the length of a string
(and any other sequence type)



The \ character after the string literal extends the string to the following line

Example:



george_v = "His majesty George v, by the grace of god " \

"of the United Kingdom of Great Britain and " \

"Ireland and of the British DOminions beyond " \

"the Seas, King, Defender of the Faith, Emperor of India"



ghandi = 'Mohandas Karamchand Ghandi'

john_f_kennedy = 'JFK'



print(len(george_v), 'characters is much too long of a name!')



print(len(ghandi), 'characters is better...

print(len(john_f_kennedy'), 'characters is short enough.')



looks like:

185 characters is much too long of a name!

26 characters is better...

3 characters is short enough.



Programs commonly access an individual character of a string. As a
sequence type, every character in a string has an index, or position, starting

,at 0 from the leftmost character. For example, the 'A' in string 'ABC' is at
index 0, 'b' is at index 1, and 'C' is at index 2. A programmer can access a
character at a specific index by appending brackets [] containing the index.



Example:

alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"



user_number = int(input('Enter number to use as index: '))

print()



print('\nLetter', user_number, 'of the alphabet is', alphabet[user_number])



by looking for 2, and based on the code you will see that this looks like:



Enter umber to use as index:



Letter 2 of the alphabet is C

Letter -1 of the alphabet is Z

*NOTE: negative numbers work backwards, positive work forwards



Write an expresses that accesses the first character of the string my_country

-> my_country[0]



Assign my_var with the last character in my_str. Use a negative index



my_var = my_str[-1]

,string_1 = 'abc'

string_2 = '123'

concatenated_string = string_1 + string_2

print('Easy as', + concatenated_string)



Easy as abc123




Use input() to read a string from input into a variable "person_name" Then
use int(input()) to read a value from input as an integer into variable
person_age. Ex: If the input is Amy 4, then the output is:

In 5 years Amy will be 9

Note: Do not write a prompt for the input values



CODE:



1 person_name = input()

2 person_age = int(input())



print('In 5 years', person_name, 'will be', person_age + 5)




A list = []

example:

prices = ['$20', 14.99, 5]

, Write a statement that assigns my_var with the 3rd element of my_list



my_var = my_list[2]



append() - add new elements to a list

pop() - remove element at index number

remove() - remove element



list_example = [list]

list_example.append = ('abc')

looks like: list abc



OPERATIONS AND DESCRIPTIONS:



* len(list) = find the length of the list

* list1 + list2 = produce a new list by concatenating list2 to the end of list1

* min(list) = find the element in the list with the smallest value

* max(list) = find the element in the list with the largest value

* sum(list) = find the sum of all elements of a list (numbers only)

* list.index(val) = find the index of the first element in list whose value
matches val.

list.count(val) = count the number of occurrences of the value val in list.



Write a statement that assigns the average of the elements of prices to the
variable avg_price



avg_price = sum('prices')/len(prices)

Document information

Uploaded on
January 12, 2025
Number of pages
117
Written in
2024/2025
Type
Class notes
Professor(s)
No
Contains
It 140 - introduction to scripting - python
$25.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
3
Last sold
-



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions