Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Document preview thumbnail
Voorbeeld 4 van de 117 pagina's
College aantekeningen

Full notes for Introduction to Scripting

Document preview thumbnail
Voorbeeld 4 van de 117 pagina's

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

Voorbeeld van de inhoud

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)

Documentinformatie

Geüpload op
12 januari 2025
Aantal pagina's
117
Geschreven in
2024/2025
Type
College aantekeningen
Docent(en)
No
Bevat
It 140 - introduction to scripting - python
$25.99

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kun je een ander document kiezen. Je kunt het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Verkocht
0
Volgers
0
Items
3
Laatst verkocht
-



Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo makkelijk kan het dus zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen