Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
** Python Certification Training: https://www.edureka.co/python **
This Edureka PPT on Python Functions tutorial covers all the important aspects of functions in Python right from the introduction to what functions are, all the way till checking out the major functions and using the code-first approach to understand them better.
Agenda
Why use Functions?
What are the Functions?
Types of Python Functions
Built-in Functions in Python
User-defined Functions in Python
Python Lambda Function
Conclusion
Python Tutorial Playlist: https://goo.gl/WsBpKe
Blog Series: http://bit.ly/2sqmP4s
Follow us to never miss an update in the future.
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Edureka!
** Python Certification Training: https://www.edureka.co/python **
This Edureka PPT on Python Functions tutorial covers all the important aspects of functions in Python right from the introduction to what functions are, all the way till checking out the major functions and using the code-first approach to understand them better.
Agenda
Why use Functions?
What are the Functions?
Types of Python Functions
Built-in Functions in Python
User-defined Functions in Python
Python Lambda Function
Conclusion
Python Tutorial Playlist: https://goo.gl/WsBpKe
Blog Series: http://bit.ly/2sqmP4s
Follow us to never miss an update in the future.
Instagram: https://www.instagram.com/edureka_learning/
Facebook: https://www.facebook.com/edurekaIN/
Twitter: https://twitter.com/edurekain
LinkedIn: https://www.linkedin.com/company/edureka
After the end of lesson you will be able to learn Python basics-What Python is? Its releases. Where we can use Python? Python Features. Tokens, comments variables etc... In out next PPT you will learn how to input and get output in Python
This presentation provides the information on python including the topics Python features, applications, variables and operators in python, control statements, numbers, strings, print formatting, list and list comprehension, dictionaries, tuples, files, sets, boolean, mehtods and functions, lambda expressions and a sample project using Python.
The Key Difference between a List and a Tuple. The main difference between lists and a tuples is the fact that lists are mutable whereas tuples are immutable. A mutable data type means that a python object of this type can be modified. Let's create a list and assign it to a variable.
This Edureka Python tutorial is a part of Python Course (Python Tutorial Blog: https://goo.gl/wd28Zr) and will help you in understanding what exactly is Python and its various applications. It also explains few Python code basics like data types, operators etc. Below are the topics covered in this tutorial:
1. Introduction to Python
2. Various Python Features
3. Python Applications
4. Python for Web Scraping
5. Python for Testing
6. Python for Web Development
7. Python for Data Analysis
Python is a popular programming language used in a variety of applications, including data analysis, web development, and artificial intelligence. Here's an introduction to the Basics of Python - A Beginners Guide! Whether you're new to programming or looking to brush up on your skills, this video covers the basics of Python programming language. From data types and operators to loops, functions and libraries, you'll get a solid foundation to start coding in Python.
Visit us: https://www.elewayte.com/
All data values in Python are encapsulated in relevant object classes. Everything in Python is an object and every object has an identity, a type, and a value. Like another object-oriented language such as Java or C++, there are several data types which are built into Python. Extension modules which are written in C, Java, or other languages can define additional types.
To determine a variable's type in Python you can use the type() function. The value of some objects can be changed. Objects whose value can be changed are called mutable and objects whose value is unchangeable (once they are created) are called immutable.
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
( Python Training : https://www.edureka.co/python )
This Edureka Python Class tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you understand Python Classes and Objects with examples. It will also explain the concept of Abstract Classes and Inheritance in python.
Check out our Python Training Playlist: https://goo.gl/Na1p9G
This Python Programming tutorial video helps you to learn following topics:
1. Python Classes and Objects
2. Inheritance
3. Abstract Classes
After the end of lesson you will be able to learn Python basics-What Python is? Its releases. Where we can use Python? Python Features. Tokens, comments variables etc... In out next PPT you will learn how to input and get output in Python
This presentation provides the information on python including the topics Python features, applications, variables and operators in python, control statements, numbers, strings, print formatting, list and list comprehension, dictionaries, tuples, files, sets, boolean, mehtods and functions, lambda expressions and a sample project using Python.
The Key Difference between a List and a Tuple. The main difference between lists and a tuples is the fact that lists are mutable whereas tuples are immutable. A mutable data type means that a python object of this type can be modified. Let's create a list and assign it to a variable.
This Edureka Python tutorial is a part of Python Course (Python Tutorial Blog: https://goo.gl/wd28Zr) and will help you in understanding what exactly is Python and its various applications. It also explains few Python code basics like data types, operators etc. Below are the topics covered in this tutorial:
1. Introduction to Python
2. Various Python Features
3. Python Applications
4. Python for Web Scraping
5. Python for Testing
6. Python for Web Development
7. Python for Data Analysis
Python is a popular programming language used in a variety of applications, including data analysis, web development, and artificial intelligence. Here's an introduction to the Basics of Python - A Beginners Guide! Whether you're new to programming or looking to brush up on your skills, this video covers the basics of Python programming language. From data types and operators to loops, functions and libraries, you'll get a solid foundation to start coding in Python.
Visit us: https://www.elewayte.com/
All data values in Python are encapsulated in relevant object classes. Everything in Python is an object and every object has an identity, a type, and a value. Like another object-oriented language such as Java or C++, there are several data types which are built into Python. Extension modules which are written in C, Java, or other languages can define additional types.
To determine a variable's type in Python you can use the type() function. The value of some objects can be changed. Objects whose value can be changed are called mutable and objects whose value is unchangeable (once they are created) are called immutable.
Python Class | Python Programming | Python Tutorial | EdurekaEdureka!
( Python Training : https://www.edureka.co/python )
This Edureka Python Class tutorial (Python Tutorial Blog: https://goo.gl/wd28Zr) will help you understand Python Classes and Objects with examples. It will also explain the concept of Abstract Classes and Inheritance in python.
Check out our Python Training Playlist: https://goo.gl/Na1p9G
This Python Programming tutorial video helps you to learn following topics:
1. Python Classes and Objects
2. Inheritance
3. Abstract Classes
Dear readers, these Python Programming Language Interview Questions have been designed specially to get you acquainted with the nature of questions you may encounter during your interview for the subject of Python Programming Language. As per my experience good interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the subject and later they continue based on further discussion and what you answer −
Essentials of Automations: Optimizing FME Workflows with ParametersSafe Software
Are you looking to streamline your workflows and boost your projects’ efficiency? Do you find yourself searching for ways to add flexibility and control over your FME workflows? If so, you’re in the right place.
Join us for an insightful dive into the world of FME parameters, a critical element in optimizing workflow efficiency. This webinar marks the beginning of our three-part “Essentials of Automation” series. This first webinar is designed to equip you with the knowledge and skills to utilize parameters effectively: enhancing the flexibility, maintainability, and user control of your FME projects.
Here’s what you’ll gain:
- Essentials of FME Parameters: Understand the pivotal role of parameters, including Reader/Writer, Transformer, User, and FME Flow categories. Discover how they are the key to unlocking automation and optimization within your workflows.
- Practical Applications in FME Form: Delve into key user parameter types including choice, connections, and file URLs. Allow users to control how a workflow runs, making your workflows more reusable. Learn to import values and deliver the best user experience for your workflows while enhancing accuracy.
- Optimization Strategies in FME Flow: Explore the creation and strategic deployment of parameters in FME Flow, including the use of deployment and geometry parameters, to maximize workflow efficiency.
- Pro Tips for Success: Gain insights on parameterizing connections and leveraging new features like Conditional Visibility for clarity and simplicity.
We’ll wrap up with a glimpse into future webinars, followed by a Q&A session to address your specific questions surrounding this topic.
Don’t miss this opportunity to elevate your FME expertise and drive your projects to new heights of efficiency.
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Ramesh Iyer
In today's fast-changing business world, Companies that adapt and embrace new ideas often need help to keep up with the competition. However, fostering a culture of innovation takes much work. It takes vision, leadership and willingness to take risks in the right proportion. Sachin Dev Duggal, co-founder of Builder.ai, has perfected the art of this balance, creating a company culture where creativity and growth are nurtured at each stage.
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Elevating Tactical DDD Patterns Through Object CalisthenicsDorra BARTAGUIZ
After immersing yourself in the blue book and its red counterpart, attending DDD-focused conferences, and applying tactical patterns, you're left with a crucial question: How do I ensure my design is effective? Tactical patterns within Domain-Driven Design (DDD) serve as guiding principles for creating clear and manageable domain models. However, achieving success with these patterns requires additional guidance. Interestingly, we've observed that a set of constraints initially designed for training purposes remarkably aligns with effective pattern implementation, offering a more ‘mechanical’ approach. Let's explore together how Object Calisthenics can elevate the design of your tactical DDD patterns, offering concrete help for those venturing into DDD for the first time!
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionAggregage
Join Maher Hanafi, VP of Engineering at Betterworks, in this new session where he'll share a practical framework to transform Gen AI prototypes into impactful products! He'll delve into the complexities of data collection and management, model selection and optimization, and ensuring security, scalability, and responsible use.
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
Key Trends Shaping the Future of Infrastructure.pdfCheryl Hung
Keynote at DIGIT West Expo, Glasgow on 29 May 2024.
Cheryl Hung, ochery.com
Sr Director, Infrastructure Ecosystem, Arm.
The key trends across hardware, cloud and open-source; exploring how these areas are likely to mature and develop over the short and long-term, and then considering how organisations can position themselves to adapt and thrive.
PHP Frameworks: I want to break free (IPC Berlin 2024)Ralf Eggert
In this presentation, we examine the challenges and limitations of relying too heavily on PHP frameworks in web development. We discuss the history of PHP and its frameworks to understand how this dependence has evolved. The focus will be on providing concrete tips and strategies to reduce reliance on these frameworks, based on real-world examples and practical considerations. The goal is to equip developers with the skills and knowledge to create more flexible and future-proof web applications. We'll explore the importance of maintaining autonomy in a rapidly changing tech landscape and how to make informed decisions in PHP development.
This talk is aimed at encouraging a more independent approach to using PHP frameworks, moving towards a more flexible and future-proof approach to PHP development.
2. Been using print to see what programs are doing
Python Input and Output
3. Been using print to see what programs are doing
How to save data to files?
Python Input and Output
4. Been using print to see what programs are doing
How to save data to files?
And read ddaattaa ffrroomm tthheemm??
Python Input and Output
5. Been using print to see what programs are doing
How to save data to files?
And read ddaattaa ffrroomm tthheemm??
Python's solution looks very much like C's
Python Input and Output
6. Been using print to see what programs are doing
How to save data to files?
And read ddaattaa ffrroomm tthheemm??
Python's solution looks very much like C's
– A file is a sequence of bytes
Python Input and Output
7. Been using print to see what programs are doing
How to save data to files?
And read ddaattaa ffrroomm tthheemm??
Python's solution looks very much like C's
– A file is a sequence of bytes
– But it's often more useful to treat it as a sequence
of lines
Python Input and Output
8. Sample data file
Three things are certain:
Death, taxes, and lost data.
Guess wwhhiicchh hhaass ooccccuurrrreedd..
Errors have occurred.
We won't tell you where or why.
Lazy programmers.
With searching comes loss
and the presence of absence:
"My Thesis" not found.
A crash reduces
your expensive computer
to a simple stone.
Python Input and Output
11. How many characters in a file?
bytes Assume 1-to-1 for now
Python Input and Output
12. How many characters in a file?
bytes Assume 1-to-1 for now
RReevviissiitt llaatteerr
Python Input and Output
13. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Python Input and Output
14. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Create a file object
Python Input and Output
15. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
File to connect to
Python Input and Output
16. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
To read
Python Input and Output
17. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Now holds file object
Python Input and Output
18. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Read entire content
of file into a string
Python Input and Output
19. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Now has a copy of
all the bytes that were
in the file
Python Input and Output
20. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Disconnect from the file
Python Input and Output
21. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Disconnect from the file
Not strictly necessary
in small programs, but
good practice
Python Input and Output
22. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Report how many
characters were read
Python Input and Output
23. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
Report how many
characters were read
bytes
Python Input and Output
24. How many characters in a file?
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
print len(data)
293
Python Input and Output
25. If the file might be large, better to read in chunks
Python Input and Output
26. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Python Input and Output
27. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
Read (at wwwwhhhhiiiilllleeee data != '': most) 64 bytes
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Python Input and Output
28. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
Read (at wwwwhhhhiiiilllleeee data != '': most) 64 bytes
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Or the empty string
if there is no more data
Python Input and Output
29. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Keep looping as long as
the last read returned
some data
Python Input and Output
30. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Do something with
the data
Python Input and Output
31. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
(Try to) reload
Python Input and Output
32. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
Should be 0 (or the loop
would still be running)
Python Input and Output
33. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
64
64
64
64
37
0
Python Input and Output
34. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
64
64
Don't do this unless
64
64
37
0
Python Input and Output
35. If the file might be large, better to read in chunks
reader = open('haiku.txt', 'r')
data = reader.read(64)
Don't do this unless the file really
might be very large (or infinite)
wwwwhhhhiiiilllleeee data != '':
pppprrrriiiinnnntttt len(data)
data = reader.read(64)
pppprrrriiiinnnntttt len(data)
reader.close()
64
64
64 64
37
0
Python Input and Output
36. More common to read one line at a time
Python Input and Output
37. More common to read one line at a time
reader = open('haiku.txt', 'r')
line = reader.readline()
total = 0
count = 0
wwwwhhhhiiiilllleeee line != '':
count += 1
total += len(line)
line = reader.readline()
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
38. More common to read one line at a time
reader = open('haiku.txt', 'r')
line = reader.readline() Read a single line
total = 0
count = 0
wwwwhhhhiiiilllleeee line != '':
count += 1
total += len(line)
line = reader.readline()
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
39. More common to read one line at a time
reader = open('haiku.txt', 'r')
line = reader.readline()
total = 0
count = 0
wwwwhhhhiiiilllleeee line != '':
count += 1
total += len(line)
line = reader.readline()
reader.close()
Keep looping until
no more lines in file
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
40. More common to read one line at a time
reader = open('haiku.txt', 'r')
line = reader.readline()
total = 0
count = 0
wwwwhhhhiiiilllleeee line != '':
count += 1
total += len(line)
line = reader.readline()
reader.close()
(Try to) reload
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
41. More common to read one line at a time
reader = open('haiku.txt', 'r')
line = reader.readline()
total = 0
count = 0
wwwwhhhhiiiilllleeee line != '':
count += 1
total += len(line)
line = reader.readline()
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
19.53333333
Python Input and Output
43. Often more convenient to read all lines at once
reader = open('haiku.txt', 'r')
contents = reader.readlines()
reader.close()
total = 0
count = 0
ffffoooorrrr line iiiinnnn contents:
count += 1
total += len(line)
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
44. Often more convenient to read all lines at once
reader = open('haiku.txt', 'r')
contents = reader.readlines() All lines in file
reader.close()
total = 0
count = 0
ffffoooorrrr line iiiinnnn contents:
count += 1
total += len(line)
as list of strings
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
45. Often more convenient to read all lines at once
reader = open('haiku.txt', 'r')
contents = reader.readlines() Loop over lines
reader.close()
total = 0
count = 0
ffffoooorrrr line iiiinnnn contents:
count += 1
total += len(line)
with for
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
46. Often more convenient to read all lines at once
reader = open('haiku.txt', 'r')
contents = reader.readlines()
reader.close()
total = 0
count = 0
ffffoooorrrr line iiiinnnn contents:
count += 1
total += len(line)
pppprrrriiiinnnntttt 'average', float(total) / float(count)
19.53333333
Python Input and Output
47. "Read lines as list" + "loop over list" is common idiom
Python Input and Output
48. "Read lines as list" + "loop over list" is common idiom
So Python provides "loop over lines in file"
Python Input and Output
49. "Read lines as list" + "loop over list" is common idiom
So Python provides "loop over lines in file"
reader = open('haiku.txt', 'r')
total = 0
count = 0
ffffoooorrrr line iiiinnnn reader:
count += 1
total += len(line)
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
50. "Read lines as list" + "loop over list" is common idiom
So Python provides "loop over lines in file"
reader = open('haiku.txt', 'r')
total = 0
count = 0
ffffoooorrrr line iiiinnnn reader:
count += 1
total += len(line)
Assign lines of text in file
to loop variable one by one
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
Python Input and Output
51. "Read lines as list" + "loop over list" is common idiom
So Python provides "loop over lines in file"
reader = open('haiku.txt', 'r')
total = 0
count = 0
ffffoooorrrr line iiiinnnn reader:
count += 1
total += len(line)
reader.close()
pppprrrriiiinnnntttt 'average', float(total) / float(count)
19.53333333
Python Input and Output
53. Put data in a file using write or writelines
Python Input and Output
54. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
Python Input and Output
55. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
Same function
Python Input and Output
56. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
File to write to
Python Input and Output
57. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
File to write to
Created if it doesn't exist
Python Input and Output
58. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
For writing instead
of reading
Python Input and Output
59. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
Write a single string
Python Input and Output
60. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
Write each string
in a list
Python Input and Output
61. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
elementsHeNeArKr
Python Input and Output
62. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elements')
writer.writelines(['He', 'Ne', 'Ar', 'Kr'])
writer.close()
elementsHeNeArKr
Python only writes what you tell it to
Python Input and Output
63. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elementsn')
writer.writelines(['Hen', 'Nen', 'Arn', 'Krn'])
writer.close()
Have to provide end-of-line characters yourself
Python Input and Output
64. Put data in a file using write or writelines
writer = open('temp.txt', 'w')
writer.write('elementsn')
writer.writelines(['Hen', 'Nen', 'Arn', 'Krn'])
writer.close()
elements
He
Ne
Ar
Kr
Python Input and Output
66. Often simpler to use print >>
writer = open('temp.txt', 'w')
pppprrrriiiinnnntttt >> writer, 'elements'
ffffoooorrrr gas iiiinnnn ['He', 'Ne', 'Ar', 'Kr']:
pppprrrriiiinnnntttt >> writer, gas
writer.close()
Python Input and Output
67. Often simpler to use print >>
writer = open('temp.txt', 'w')
pppprrrriiiinnnntttt >> writer, 'elements'
ffffoooorrrr gas iiiinnnn ['He', 'Ne', 'Ar', 'Kr']:
pppprrrriiiinnnntttt >> writer, gas
writer.close()
Specify open file after >>
Python Input and Output
68. Often simpler to use print >>
writer = open('temp.txt', 'w')
pppprrrriiiinnnntttt >> writer, 'elements'
ffffoooorrrr gas iiiinnnn ['He', 'Ne', 'Ar', 'Kr']:
pppprrrriiiinnnntttt >> writer, gas
writer.close()
print automatically adds the newline
Python Input and Output
70. Copy a file
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
writer = open('temp.txt', 'w')
write.write(data)
writer.close()
Python Input and Output
71. Copy a file
reader = open('haiku.txt', 'r')
data = reader.read() Read all
reader.close()
writer = open('temp.txt', 'w')
write.write(data)
writer.close()
Python Input and Output
72. Copy a file
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
writer = open('temp.txt', 'w')
write.write(data)
writer.close()
Write all
Python Input and Output
73. Copy a file
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
writer = open('temp.txt', 'w')
write.write(data)
writer.close()
Probably won't work with a terabyte...
Python Input and Output
74. Copy a file
reader = open('haiku.txt', 'r')
data = reader.read()
reader.close()
writer = open('temp.txt', 'w')
write.write(data)
writer.close()
Probably won't work with a terabyte...
…but we probably don't care
Python Input and Output
75. Copy a file (version 2)
Python Input and Output
76. Copy a file (version 2)
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
writer.write(line)
reader.close()
writer.close()
Python Input and Output
77. Copy a file (version 2)
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
writer.write(line)
reader.close()
writer.close()
Assumes the file is text
Python Input and Output
78. Copy a file (version 2)
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
writer.write(line)
reader.close()
writer.close()
Assumes the file is text
Or at least that the end-of-line character appears
frequently
Python Input and Output
80. This version doesn't make an exact copy
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
print >> writer, line
reader.close()
writer.close()
Python Input and Output
81. This version doesn't make an exact copy
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
print >> writer, line
reader.close()
writer.close()
Python keeps the newline when reading
Python Input and Output
82. This version doesn't make an exact copy
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
print >> writer, line
reader.close()
writer.close()
Python keeps the newline when reading
print automatically adds a newline
Python Input and Output
83. This version doesn't make an exact copy
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
ffffoooorrrr line iiiinnnn reader:
print >> writer, line
reader.close()
writer.close()
Python keeps the newline when reading
print automatically adds a newline
Result is double-spaced output
Python Input and Output
84. Copy a file (version 3)
Python Input and Output
85. Copy a file (version 3)
BLOCKSIZE = 1024
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
data = reader.read(BLOCKSIZE)
wwwwhhhhiiiilllleeee len(data) > 0:
writer.write(data)
data = reader.read(BLOCKSIZE)
reader.close()
writer.close()
Python Input and Output
86. Copy a file (version 3)
BLOCKSIZE = 1024
reader = open('haiku.txt', 'r')
writer = open('temp.txt', 'w')
data = reader.read(BLOCKSIZE)
wwwwhhhhiiiilllleeee len(data) > 0:
writer.write(data)
data = reader.read(BLOCKSIZE)
reader.close()
writer.close()
(Needlessly?) harder to understand
Python Input and Output