Download presentation
Presentation is loading. Please wait.
1
Intro to Programming Lecture 3
CIT 590 Intro to Programming Lecture 3
2
Agenda Style More examples of functions Few notes about style
Variable names The right way to end a program in python comments More examples of functions Few notes about style String slicing Functions returning nothing Code walkthroughs cokeMachine.py romanNumerals.py
3
Function style Give functions reasonable names – sometimes might be hard to come up with an elegant name (see 3 for a possible solution) Define functions in their own separate block – Python is super flexible and allows you to do this anywhere, but that affects readability Put down a docString – a comment that explains what the function does. Triple quoted string to begin the function
4
Strings and string functions
name = ‘superman’ len(name) for length name[0] ‘s’ Slicing – to get portions of a string (or list) Positive indices name[1:3] Includes the first index and excludes the ending index Negative index means grab characters from the end name[-1] is the last character
5
String and string functions
More slicing name[1:] name[:5] name[:-5] Copying a string name[:] gives you all the character of the string Looping through characters in a string for ch in string: The ‘+’ operator does concatenation for strings
6
Functions that return nothing
The NoneType generally seen in functions with only output used fairly often to initialize the value of a variable when you do not know what it might eventually hold
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.